-
Notifications
You must be signed in to change notification settings - Fork 1
/
Palindrome Partitioning(min cuts)
158 lines (134 loc) · 4.54 KB
/
Palindrome Partitioning(min cuts)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
bool isPalindrome(string String, int i, int j)
{
while(i < j)
{
if(String[i] != String[j])
return false;
i++;
j--;
}
return true;
}
int minPalPartion(string String, int i, int j)
{
if( i >= j || isPalindrome(String, i, j) )
return 0;
int ans = INT_MAX, count;
for(int k = i; k < j; k++)
{
count = minPalPartion(String, i, k) +
minPalPartion(String, k + 1, j) + 1;
ans = min(ans, count);
}
return ans;
}
// Driver code
int main() {
string str = "ababbbabbababa";
cout << "Min cuts needed for " <<
"Palindrome Partitioning is " <<
minPalPartion(str, 0, str.length() - 1) << endl;
return 0;
}
DP-O(n^3)
int minPalPartion(string str)
{
// Get the length of the string
int n = str.length();
/* Create two arrays to build the solution
in bottom up manner
C[i][j] = Minimum number of cuts needed for
palindrome partitioning
of substring str[i..j]
P[i][j] = true if substring str[i..j] is
palindrome, else false
Note that C[i][j] is 0 if P[i][j] is true */
int C[n][n];
bool P[n][n];
// Every substring of length 1 is a palindrome
for (int i = 0; i < n; i++) {
P[i][i] = true;
C[i][i] = 0;
}
/* L is substring length. Build the
solution in bottom up manner by
considering all substrings of
length starting from 2 to n.
The loop structure is same as Matrx
Chain Multiplication problem
( See https:// www.geeksforgeeks.org/matrix-chain-multiplication-dp-8/ )*/
for (int L = 2; L <= n; L++) {
// For substring of length L, set
// different possible starting indexes
for (int i = 0; i < n - L + 1; i++) {
int j = i + L - 1; // Set ending index
// If L is 2, then we just need to
// compare two characters. Else
// need to check two corner characters
// and value of P[i+1][j-1]
if (L == 2)
P[i][j] = (str[i] == str[j]);
else
P[i][j] = (str[i] == str[j]) && P[i + 1][j - 1];
// IF str[i..j] is palindrome, then C[i][j] is 0
if (P[i][j] == true)
C[i][j] = 0;
else {
// Make a cut at every possible
// location starting from i to j,
// and get the minimum cost cut.
C[i][j] = INT_MAX;
for (int k = i; k <= j - 1; k++)
C[i][j] = min(C[i][j], C[i][k] + C[k + 1][j] + 1);
}
}
}
// Return the min cut value for
// complete string. i.e., str[0..n-1]
return C[0][n - 1];
}
In the above approach, we can calculate the minimum cut while finding all palindromic substring.
If we find all palindromic substring 1st and then we calculate minimum cut, time complexity will reduce to O(n2).
int palindromicPartition(string str)
{
int n = str.size();
/* Create two arrays to build the solution in bottom-up manner
C[i] = Minimum number of cuts needed for a palindrome partitioning
of substring str[0..i]
P[i][j] = true if substring str[i..j] is palindrome, else false
Note that C[i] is 0 if P[0][i] is true */
vector<int> C(n);
vector<vector<bool>> P(n,vector<bool>(n));
int i, j, k, L; // different looping variables
// Every substring of length 1 is a palindrome
for (i = 0; i < n; i++) {
P[i][i] = true;
}
/* L is substring length. Build the solution in bottom up manner by
considering all substrings of length starting from 2 to n. */
for (L = 2; L <= n; L++) {
// For substring of length L, set different possible starting indexes
for (i = 0; i < n - L + 1; i++) {
j = i + L - 1; // Set ending index
// If L is 2, then we just need to compare two characters. Else
// need to check two corner characters and value of P[i+1][j-1]
if (L == 2)
P[i][j] = (str[i] == str[j]);
else
P[i][j] = (str[i] == str[j]) && P[i + 1][j - 1];
}
}
for (i = 0; i < n; i++) {
if (P[0][i] == true)
C[i] = 0;
else {
C[i] = INT_MAX;
for (j = 0; j < i; j++) {
if (P[j + 1][i] == true && 1 + C[j] < C[i])
C[i] = 1 + C[j];
}
}
}
// Return the min cut value for complete string. i.e., str[0..n-1]
return C[n - 1];
}