Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active September 8, 2022 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/3f0daeeae6b3b3976e75 to your computer and use it in GitHub Desktop.
Save thmain/3f0daeeae6b3b3976e75 to your computer and use it in GitHub Desktop.
public int splitRecursion(String x){
if(x=="" || isPalindrome(x)){
// System.out.println(x);
return 0;
}else{
int cuts = Integer.MAX_VALUE;
for (int i = 1; i <x.length() ; i++) {
cuts = Math.min(1+ splitRecursion(x.substring(0, i)) + splitRecursion(x.substring(i, x.length())),cuts);
}
return cuts;
}
}
public boolean isPalindrome(String s){
int n = s.length();
for (int i=0;i<(n / 2) + 1;++i) {
if (s.charAt(i) != s.charAt(n - i - 1)) {
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment