classSolution{ public List<List<String>> partition(String s) { List<List<String>> res = new ArrayList<>(); if (s.length() == 0){ res.add(new ArrayList<>()); return res; } helper(res, s, new ArrayList<String>(), 0); return res; } privatevoidhelper(List<List<String>> res, String s, List<String> temp, int index){ if (index > s.length()-1){ res.add(new ArrayList<>(temp)); } for (int i = index + 1; i <= s.length(); i++){ String cur = s.substring(index, i); if (!isPalindrome(cur)) continue; temp.add(cur); helper(res, s, temp, i); temp.remove(temp.size()-1); } } privatebooleanisPalindrome(String s){ int left = 0; int right = s.length()-1; while(left < right){ if (s.charAt(left) != s.charAt(right)) returnfalse; left ++; right --; } returntrue; } }