Skip to content

Commit d08d7e4

Browse files
committed
update DP Count of Palindromic Substrings
1 parent 0058afa commit d08d7e4

File tree

1 file changed

+77
-4
lines changed

1 file changed

+77
-4
lines changed

✅ Pattern 15: 0-1 Knapsack (Dynamic Programming).md

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3434,25 +3434,25 @@ console.log('Length of LPS ---> ' + findLPSLength('pqr'));
34343434
// Explanation: LPS could be "p", "q" or "r".
34353435
```
34363436
- The <b>time and space complexity</b> of the above algorithm is `O(n²)`, where `n` is the length of the input <i>sequence</i>.
3437-
## 👩🏽‍🦯 Longest Palindromic Substring
3437+
## 👩🏽‍🦯 🌴 Longest Palindromic Substring
34383438
https://leetcode.com/problems/longest-palindromic-substring/
34393439
34403440
> 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>.
34413441
34423442
#### Example 1:
3443-
```
3443+
```js
34443444
Input: "abdbca"
34453445
Output: 3
34463446
Explanation: LPS is "bdb".
34473447
```
34483448
#### Example 2:
3449-
```
3449+
```js
34503450
Input: = "cddpd"
34513451
Output: 3
34523452
Explanation: LPS is "dpd".
34533453
```
34543454
#### Example 3:
3455-
```
3455+
```js
34563456
Input: = "pqr"
34573457
Output: 1
34583458
Explanation: LPS could be "p", "q" or "r".
@@ -3634,6 +3634,79 @@ The best-known algorithm to find the <b>[longest palindromic substring](#👩
36343634
## 👩🏽‍🦯 Count of Palindromic Substrings
36353635
https://leetcode.com/problems/palindromic-substrings/
36363636
3637+
> 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+
function findCPS(str) {
3666+
// dp[i][j] will be 'true' if the string from index 'i' to index 'j' is a palindrome
3667+
const dp = 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+
36373710
## 🔎 Minimum Deletions in a String to make it a Palindrome
36383711
https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/
36393712

0 commit comments

Comments
 (0)