You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> Given a string, find the length of its <b>Longest Palindromic Substring (LPS)</b>. In a <i>palindromic</i> string, <i>elements read the same backward and forward</i>.
3441
3441
3442
3442
#### Example 1:
3443
-
```
3443
+
```js
3444
3444
Input:"abdbca"
3445
3445
Output:3
3446
3446
Explanation:LPS is "bdb".
3447
3447
```
3448
3448
#### Example 2:
3449
-
```
3449
+
```js
3450
3450
Input:="cddpd"
3451
3451
Output:3
3452
3452
Explanation:LPS is "dpd".
3453
3453
```
3454
3454
#### Example 3:
3455
-
```
3455
+
```js
3456
3456
Input:="pqr"
3457
3457
Output:1
3458
3458
Explanation:LPS could be "p", "q" or "r".
@@ -3634,6 +3634,79 @@ The best-known algorithm to find the <b>[longest palindromic substring](#👩
> Given a <i>string</i>, find the total number of <i>palindromic substrings</i> in it. Please note we need to find the total number of <i>substrings</i> and not <i>subsequences</i>.
3638
+
3639
+
#### Example 1:
3640
+
```js
3641
+
Input:"abdbca"
3642
+
Output:7
3643
+
Explanation: Here are the palindromic substrings, "a", "b", "d", "b", "c", "a", "bdb".
3644
+
```
3645
+
#### Example 2:
3646
+
```js
3647
+
Input:="cddpd"
3648
+
Output:7
3649
+
Explanation: Here are the palindromic substrings, "c", "d", "d", "p", "d", "dd", "dpd".
3650
+
```
3651
+
#### Example 3:
3652
+
```js
3653
+
Input:="pqr"
3654
+
Output:3
3655
+
Explanation: Here are the palindromic substrings,"p", "q", "r".
3656
+
```
3657
+
3658
+
This problem follows the <b>[Longest Palindromic Subsequence pattern](#pattern-4-palindromic-subsequence)</b>and can be easily converted to <b>[Longest Palindromic Substring](#👩🏽🦯-🌴-longest-palindromic-substring)</b>. The only difference is that instead of calculating the <b>longest palindromic substring</b>, we will instead count all the <b>palindromic substrings</b>.
3659
+
3660
+
### Bottom-up Dynamic Programming
3661
+
3662
+
Let’s jump directly to the <b>bottom-up dynamic programming</b> solution:
3663
+
3664
+
```js
3665
+
functionfindCPS(str) {
3666
+
// dp[i][j] will be 'true' if the string from index 'i' to index 'j' is a palindrome
3667
+
constdp=Array(str.length)
3668
+
.fill(false)
3669
+
.map(() =>Array(str.length).fill(false));
3670
+
3671
+
let count =0;
3672
+
3673
+
//every string with one character is a palindrome
3674
+
for (let i =0; i <str.length; i++) {
3675
+
dp[i][i] =true;
3676
+
count++;
3677
+
}
3678
+
3679
+
for (let startIndex =str.length-1; startIndex >=0; startIndex--) {
3680
+
for (let endIndex = startIndex +1; endIndex <str.length; endIndex++) {
3681
+
if (str.charAt(startIndex) ===str.charAt(endIndex)) {
3682
+
//if it's a two character string or if the remaining string is palindrome too
3683
+
if (endIndex - startIndex ===1|| dp[startIndex +1][endIndex -1]) {
3684
+
dp[startIndex][endIndex] =true;
3685
+
count++;
3686
+
}
3687
+
}
3688
+
}
3689
+
}
3690
+
console.log(dp);
3691
+
return count;
3692
+
}
3693
+
3694
+
console.log('Length of LPS: ---> '+findCPS('abdbca'));
3695
+
// Output: 7
3696
+
// Explanation: Here are the palindromic substrings, "a", "b", "d", "b", "c", "a", "bdb".
3697
+
3698
+
console.log('Length of LPS: ---> '+findCPS('cddpd'));
3699
+
// Output: 7
3700
+
// Explanation: Here are the palindromic substrings, "c", "d", "d", "p", "d", "dd", "dpd".
3701
+
3702
+
console.log('Length of LPS: ---> '+findCPS('pqr'));
3703
+
// Output: 3
3704
+
// Explanation: Here are the palindromic substrings,"p", "q", "r".
3705
+
```
3706
+
3707
+
- The <b>time and space complexity</b> of the above algorithm is `O(n²)`, where `n` is the length of the input string.
3708
+
3709
+
3637
3710
## 🔎 Minimum Deletions in a String to make it a Palindrome
0 commit comments