-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSplitPalindrome.java
More file actions
46 lines (39 loc) · 1.02 KB
/
SplitPalindrome.java
File metadata and controls
46 lines (39 loc) · 1.02 KB
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
package com.ub.dynamic;
public class SplitPalindrome {
private boolean isPalindrome(String src) {
char [] srcChar = src.toCharArray();
int len = src.length();
for(int i=0,j=len-1;i<len/2;i++,j--) {
if(srcChar[i] != srcChar[j])
return false;
}
return true;
}
private int palindromeSplit(String src) {
// int count = 1;
if(isPalindrome(src) == true) {
return 1;
}else {
int len = src.length();
int count = len;
for(int i=1;i<len;i++) {
count = Math.min(palindromeSplit(src.substring(0, i))
+ palindromeSplit(src.substring(i,len-1)),count);
}
return count;
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SplitPalindrome splt = new SplitPalindrome();
boolean ans;
ans = splt.isPalindrome("TAT");
ans = splt.isPalindrome("NAMAN");
ans = splt.isPalindrome("KARAN");
int cnt = splt.palindromeSplit("abacdcefe");
System.out.println("Ans is ="+cnt);
}
}