classSolution { public List<List<String>> partition(String s) { List<List<String>> result = newLinkedList<>(); partition(s, 0, newLinkedList<>(), result); return result; }
privatevoidpartition(String str, int index, List<String> current, List<List<String>> result) { if (index == str.length()) { result.add(newLinkedList<>(current)); return; }
for (inti= index + 1; i <= str.length(); i++) { if (isPalindrome(str, index, i - 1)) { current.add(str.substring(index, i)); partition(str, i, current, result); current.remove(current.size() - 1); } } }
privatebooleanisPalindrome(String str, int start, int end) { intlen= (end - start + 1) / 2; for (inti=0; i < len; i++) { if (str.charAt(start + i) != str.charAt(end - i)) { returnfalse; } }