http://allenlipeng47.com/PersonalPage/index/view/194/nkey
public static List<List<String>> getPermutation(String str) {
List<List<String>> result = new ArrayList<List<String>>();
getPermutationUtil(str, 0, result, new ArrayList<String>());
return result;
}
public static void getPermutationUtil(String str, int start, List<List<String>> result, List<String> currResult) {
if(start==str.length()) {
ArrayList<String> curr = new ArrayList<String>(currResult);
result.add(curr);
}
else {
for(int i=start; i<str.length(); i++) {
String currStr = str.substring(start, i+1);
currResult.add(currStr);
getPermutationUtil(str, i + 1, result, currResult);
currResult.remove(currResult.size()-1);
}
}
}
https://github.com/allenlipeng47/algorithm/blob/master/src/main/java/com/pli/project/algorithm/array/SuperSet.java
public class SuperSet {
public static void main(String[] args) {
char[] chs = {'a', 'b', 'c', 'd'};
// printSuperSet(chs);
List<List<Character>> result = getSuperSet(chs);
System.out.println(result);
}
public static List<List<Character>> getSuperSet(char[] chs) {
List<List<Character>> result = new ArrayList<List<Character>>();
getSuperSetUtil(chs, 0, result, new ArrayList<Character>());
return result;
}
public static void getSuperSetUtil(char[] chs, int start, List<List<Character>> result, List<Character> currResult) {
if(start==chs.length) {
ArrayList<Character> curr = new ArrayList<Character>(currResult);
result.add(curr);
}
else {
//count current char
currResult.add(chs[start]);
getSuperSetUtil(chs, start + 1, result, currResult);
currResult.remove(currResult.size()-1);
//doesn't count current char
getSuperSetUtil(chs, start+1, result, currResult);
}
}
public static void printSuperSet(char[] chs) {
List<List<Character>> sets = new ArrayList<List<Character>>();
int size = (int)Math.pow(2, chs.length);
for(int i=0; i<size; i++) {
List<Character> list = new ArrayList<Character>();
for(int j=0; j<chs.length; j++) {
if(((1<<j)&i)>0) {
list.add(chs[j]);
}
}
sets.add(list);
}
System.out.println(sets);
}
}
https://github.com/allenlipeng47/algorithm/blob/master/src/main/java/com/pli/project/algorithm/array/SuperSet.java
public static void main(String[] args) {
String str = "abacbdbc";
System.out.println(getAllPalindromePartition(str));
}
public static List<List<String>> getAllPalindromePartition(String str) {
List<List<String>> result = new ArrayList<List<String>>();
getAllPalindromePartitionUtil(str, 0, result, new ArrayList<String>());
return result;
}
public static void getAllPalindromePartitionUtil(String str, int start, List<List<String>> result, List<String> currResult) {
if(start==str.length()) {
ArrayList<String> curr = new ArrayList<String>(currResult);
result.add(curr);
}
else {
for(int i=start; i<str.length(); i++) {
String currStr = str.substring(start, i+1);
if(isPalindrome(currStr)) {
currResult.add(currStr);
getAllPalindromePartitionUtil(str, i + 1, result, currResult);
currResult.remove(currResult.size()-1);
}
}
}
}
public static boolean isPalindrome(String str) {
int left=0, right=str.length()-1;
while(left<right) {
if(str.charAt(left)==str.charAt(right)) {
left++;
right--;
}
else {
return false;
}
}
return true;
}
}
public static List<List<String>> getPermutation(String str) {
List<List<String>> result = new ArrayList<List<String>>();
getPermutationUtil(str, 0, result, new ArrayList<String>());
return result;
}
public static void getPermutationUtil(String str, int start, List<List<String>> result, List<String> currResult) {
if(start==str.length()) {
ArrayList<String> curr = new ArrayList<String>(currResult);
result.add(curr);
}
else {
for(int i=start; i<str.length(); i++) {
String currStr = str.substring(start, i+1);
currResult.add(currStr);
getPermutationUtil(str, i + 1, result, currResult);
currResult.remove(currResult.size()-1);
}
}
}
https://github.com/allenlipeng47/algorithm/blob/master/src/main/java/com/pli/project/algorithm/array/SuperSet.java
public class SuperSet {
public static void main(String[] args) {
char[] chs = {'a', 'b', 'c', 'd'};
// printSuperSet(chs);
List<List<Character>> result = getSuperSet(chs);
System.out.println(result);
}
public static List<List<Character>> getSuperSet(char[] chs) {
List<List<Character>> result = new ArrayList<List<Character>>();
getSuperSetUtil(chs, 0, result, new ArrayList<Character>());
return result;
}
public static void getSuperSetUtil(char[] chs, int start, List<List<Character>> result, List<Character> currResult) {
if(start==chs.length) {
ArrayList<Character> curr = new ArrayList<Character>(currResult);
result.add(curr);
}
else {
//count current char
currResult.add(chs[start]);
getSuperSetUtil(chs, start + 1, result, currResult);
currResult.remove(currResult.size()-1);
//doesn't count current char
getSuperSetUtil(chs, start+1, result, currResult);
}
}
public static void printSuperSet(char[] chs) {
List<List<Character>> sets = new ArrayList<List<Character>>();
int size = (int)Math.pow(2, chs.length);
for(int i=0; i<size; i++) {
List<Character> list = new ArrayList<Character>();
for(int j=0; j<chs.length; j++) {
if(((1<<j)&i)>0) {
list.add(chs[j]);
}
}
sets.add(list);
}
System.out.println(sets);
}
}
https://github.com/allenlipeng47/algorithm/blob/master/src/main/java/com/pli/project/algorithm/array/SuperSet.java
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s =
Return
public class AllPalindromePartition {"aab"
,Return
1
since the palindrome partitioning ["aa","b"]
could be produced using 1 cut.public static void main(String[] args) {
String str = "abacbdbc";
System.out.println(getAllPalindromePartition(str));
}
public static List<List<String>> getAllPalindromePartition(String str) {
List<List<String>> result = new ArrayList<List<String>>();
getAllPalindromePartitionUtil(str, 0, result, new ArrayList<String>());
return result;
}
public static void getAllPalindromePartitionUtil(String str, int start, List<List<String>> result, List<String> currResult) {
if(start==str.length()) {
ArrayList<String> curr = new ArrayList<String>(currResult);
result.add(curr);
}
else {
for(int i=start; i<str.length(); i++) {
String currStr = str.substring(start, i+1);
if(isPalindrome(currStr)) {
currResult.add(currStr);
getAllPalindromePartitionUtil(str, i + 1, result, currResult);
currResult.remove(currResult.size()-1);
}
}
}
}
public static boolean isPalindrome(String str) {
int left=0, right=str.length()-1;
while(left<right) {
if(str.charAt(left)==str.charAt(right)) {
left++;
right--;
}
else {
return false;
}
}
return true;
}
}