public List<List<String>> partition(String s) { List<List<String>> result = new ArrayList<>(); if (s == null || s.length() == 0) { return result; } handle(s, 0, new ArrayList<>(), result); return result; }
privatevoidhandle(String str, int index, List<String> curList, List<List<String>> result){ if (index == str.length()) { result.add(new ArrayList<>(curList)); return; } for (int i = index + 1; i <= str.length(); i++) { if (isPalindrome(str.substring(index, i))) { curList.add(str.substring(index, i)); handle(str, i, curList, result); curList.remove(curList.size() - 1); } } }
privatebooleanisPalindrome(String str){ if (str == null || str.length() == 0) { returnfalse; } if (str.length() == 1) { returntrue; } for (int i = 0; i < str.length() / 2; i++) { if (str.charAt(i) != str.charAt(str.length() - i - 1)) { returnfalse; } } returntrue; }