Skip to content

Instantly share code, notes, and snippets.

@honux77
Created May 12, 2014 03:43
Show Gist options
  • Select an option

  • Save honux77/958245f7f1cb968e9631 to your computer and use it in GitHub Desktop.

Select an option

Save honux77/958245f7f1cb968e9631 to your computer and use it in GitHub Desktop.
public class Solution {
public ArrayList <ArrayList<String>> partition(String s) {
ArrayList <ArrayList<String>> ret = new ArrayList <ArrayList<String>>();
if (s == null || s=="")
return ret;
ArrayList<String> palindrome = new ArrayList<String>();
addPalindrome(s, 0, palindrome, ret);
return ret;
}
public void addPalindrome(String s, int start, ArrayList<String> p,
ArrayList <ArrayList<String>> ret) {
int e = s.length();
if (start == e) {
ArrayList <String> temp = new ArrayList<String>(p);
ret.add(temp);
return;
}
for (int i = start + 1; i <= e; i++) {
String str = s.substring(start, i);
if (isPalindrome(str)) {
p.add(str);
addPalindrome(s, i, p, ret);
//if comment this, fun
p.remove(p.size() - 1);
}
}
}
public boolean isPalindrome(String s) {
int i = 0;
int e = s.length() - 1;
while (i < e) {
if(s.charAt(i++) != s.charAt(e--))
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment