-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path36. Palindrome Partitioning Recursive
63 lines (43 loc) · 1.18 KB
/
36. Palindrome Partitioning Recursive
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
36. Palindrome Partitioning Recursive
Video Link : https://youtu.be/szKVpQtBHh8
GFG practice Link : https://practice.geeksforgeeks.org/problems/palindromic-patitioning4845/1
Code :
------->>>>>>>>>>>------------>>>>>>>>>>>>________CODE________<<<<<<<<<<<<------------<<<<<<<<<<<<<<<<<<<<<<<<<<<
class Solution{
public:
//function to check palindrome string
bool isPalindrome(string s, int i,int j)
{
while(i<j){
if(s[i]!=s[j]){
return false;
}
i++;j--;
}
return true;
}
// Apply concept of matrix chain multiplication
int solve (int i ,int j ,string s)
{
if(i>=j){
return 0;
}
if(isPalindrome(s,i,j)==true){
return 0;
}
int ans=INT_MAX;
for(int k=i;k<=j-1;k++)
{
int temp= 1+ solve(i,k,s )+ solve(k+1,j,s) ;
ans=min(ans,temp);
}
return ans;
}
int palindromicPartition(string str)
{
// code here
int i=0,n=str.length(), j=n-1;
int ans= solve(i,j,str);
return ans;
}
};