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, we want to cut it into pieces such that each piece is a <b>palindrome</b>. Write a function to return the minimum number of cuts needed.
3944
+
3945
+
#### Example 1:
3946
+
```js
3947
+
Input:"abdbca"
3948
+
Output:3
3949
+
Explanation: Palindrome pieces are "a", "bdb", "c", "a".
3950
+
```
3951
+
#### Example 2:
3952
+
```js
3953
+
Input:="cddpd"
3954
+
Output:2
3955
+
Explanation: Palindrome pieces are "c", "d", "dpd".
3956
+
```
3957
+
#### Example 3:
3958
+
```js
3959
+
Input:="pqr"
3960
+
Output:2
3961
+
Explanation: Palindrome pieces are "p", "q", "r".
3962
+
```
3963
+
#### Example 4:
3964
+
```js
3965
+
Input:="pp"
3966
+
Output:0
3967
+
Explanation: We do not need to cut, as "pp" is a palindrome.
3968
+
```
3969
+
3970
+
### Brute-Force Recursive Solution
3971
+
This problem follows the <b>[Longest Palindromic Subsequence pattern](#pattern-4-palindromic-subsequence)</b> and shares a similar approach as that of the [Longest Palindromic Substring](#longest-palindromic-subsequence).
3972
+
3973
+
The <b>brute-force solution</b> will be to try all the <i>substring combinations</i> of the given string. We can start processing from the beginning of the string and keep adding one character at a time. At any step, if we get a <i>palindrome</i>, we take it as one piece and <i>recursively</i> process the remaining length of the string to find the minimum cuts needed.
// Explanation: We do not need to cut, as "pp" is a palindrome.
4027
+
```
4028
+
- The <b>time complexity</b> of the above algorithm is exponential `O(2ⁿ)`, where `n` represents the total number.
4029
+
- The <b>space complexity</b> is `O(n)`, which will be used to store the <i>recursion stack</i>.
4030
+
### Top-down Dynamic Programming with Memoization#
4031
+
We can memoize both functions `findMPPCutsRecursive()` and `isPalindrome()`. The two changing values in both these functions are the two indexes; therefore, we can store the results of all the <i>subproblems</i> in a two-dimensional array. (alternatively, we can use a <i>hash-table</i>).
// Explanation: We do not need to cut, as "pp" is a palindrome.
4101
+
```
4102
+
### Bottom-up Dynamic Programming#
4103
+
The above solution tells us that we need to build two tables, one for the `isPalindrome()` and one for `findMPPCuts()`.
4104
+
4105
+
If you remember, we built a table in the [Longest Palindromic Substring (LPS)](#longest-palindromic-subsequence) chapter that can tell us what <i>substrings</i> (of the input <i>string</i>) are <i>palindrome</i>. We will use the same approach here to build the table required for `isPalindrome()`.
4106
+
4107
+
To build the second table for finding the minimum cuts, we can iterate through the first table built for `isPalindrome()`. At any step, if we get a <i>palindrome</i>, we can cut the <i>string</i> there. Which means minimum cuts will be one plus the cuts needed for the <i>remaining string</i>.
4108
+
4109
+
Here is the code for the <b>bottom-up approach</b>:
4110
+
```js
4111
+
functionfindMPPCuts(str) {
4112
+
// isPalindrome[i][j] will be 'true' if the string from index 'i' to index 'j' is a palindrome
4113
+
constisPalindrome=Array(str.length)
4114
+
.fill(false)
4115
+
.map(() =>Array(str.length).fill(false));
4116
+
4117
+
//every string with one character is a palindrome
4118
+
for (let i =0; i <str.length; i++) {
4119
+
isPalindrome[i][i] =true;
4120
+
}
4121
+
4122
+
//populate isPalindrome[][]
4123
+
for (let startIndex =str.length-1; startIndex >=0; startIndex--) {
4124
+
for (let endIndex = startIndex +1; endIndex <str.length; endIndex++) {
4125
+
if (str.charAt(startIndex) ===str.charAt(endIndex)) {
4126
+
//if it's a two cahracter strin or if the remaing
4127
+
//string is a palindrome too
4128
+
if (
4129
+
endIndex - startIndex ===1||
4130
+
isPalindrome[startIndex +1][endIndex -1]
4131
+
) {
4132
+
isPalindrome[startIndex][endIndex] =true;
4133
+
}
4134
+
}
4135
+
}
4136
+
}
4137
+
4138
+
//now lets populate the other [][], every index in cuts
4139
+
//stores the min cuts needed for the substring
4140
+
//from that index until the end
4141
+
constcuts=Array(str.length).fill(0);
4142
+
4143
+
for (let startIndex =str.length-1; startIndex >=0; startIndex--) {
4144
+
//maximum cuts
4145
+
let minCuts =str.length;
4146
+
4147
+
for (let endIndex =str.length-1; endIndex >= startIndex; endIndex--) {
4148
+
if (isPalindrome[startIndex][endIndex]) {
4149
+
//we can cut here as we got a palindrome
4150
+
//also we dont need any cut if the whole substring is a palindrome
0 commit comments