-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path132.txt
More file actions
31 lines (28 loc) · 726 Bytes
/
132.txt
File metadata and controls
31 lines (28 loc) · 726 Bytes
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
class Solution {
int dp[];
public int minCut(String s) {
dp=new int[s.length()];
Arrays.fill(dp,-1);
int res= dfs(s,0);
return res;
}
public int dfs(String s,int l){
if(l>=s.length())return 0;
if(check(s,l,s.length()-1))return 0;
if(dp[l]!=-1)return dp[l];
int res=Integer.MAX_VALUE;
for(int i=l;i<s.length();i++){
if(check(s,l,i)){
res=Math.min(res,1+dfs(s,i+1));
}
}
dp[l]=res;
return res;
}
public boolean check(String s,int l,int r){
while(l<r){
if(s.charAt(l++)!=s.charAt(r--))return false;
}
return true;
}
}