Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Partition Palindrome (BackTracking) #131
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { public List<List<String>> partition(String s) { List<List<String>> list = new ArrayList<>(); backtrack(list, new ArrayList<>(), s, 0); return list; } public void backtrack(List<List<String>> list, List<String> tempList, String s, int start){ if(start == s.length()) list.add(new ArrayList<>(tempList)); else{ for(int i = start; i < s.length(); i++){ if(isPalindrome(s, start, i)){ tempList.add(s.substring(start, i + 1)); backtrack(list, tempList, s, i + 1); tempList.remove(tempList.size() - 1); } } } } public boolean isPalindrome(String s, int low, int high){ while(low < high) if(s.charAt(low++) != s.charAt(high--)) return false; return true; } public static void main(String args[]) { System.out.println("Hello, World!"); Rextester obj = new Rextester(); List<List<String>> out = obj.partition("aab"); System.out.println("Size :"+out.size()); for(List<String> lst:out){ for(String s:lst){ System.out.print(s+" "); } System.out.println("-"); } } }
run
|
edit
|
history
|
help
0
Most views runner!
Aaina
Engine Java
Coding Challenge - 02 (Odd numbers)
calc exp gxb2
Use of JS in Java (not my)
JavaQ5
Merge Sort in Java
Javaprog.java
test1