Last active
December 20, 2015 22:09
-
-
Save charlespunk/6203153 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Given a string s, partition s such that every substring of the partition is a palindrome. | |
Return all possible palindrome partitioning of s. | |
For example, given s = "aab", | |
Return | |
[ | |
["aa","b"], | |
["a","a","b"] | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public ArrayList<ArrayList<String>> partition(String s) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
ArrayList<ArrayList<String>> all = new ArrayList<ArrayList<String>>(); | |
ArrayList<String> one = new ArrayList<String>(); | |
partition(s, 0, one, all); | |
return all; | |
} | |
public void partition(String s, int start, ArrayList<String> one, | |
ArrayList<ArrayList<String>> all){ | |
if(start == s.length()){ | |
all.add(new ArrayList<String>(one)); | |
return; | |
} | |
for(int i = start; i < s.length(); i++){ | |
if(isPartition(s, start, i)){ | |
one.add(s.substring(start, i + 1)); | |
partition(s, i + 1, one, all); | |
one.remove(one.size() - 1); | |
} | |
} | |
} | |
public boolean isPartition(String s, int begin, int end){ | |
while(begin <= end){ | |
if(s.charAt(begin++) != s.charAt(end--)) return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment