[LC] 131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
Example:
Input: "aab" Output: [ ["aa","b"], ["a","a","b"] ]
class Solution { public List<List<String>> partition(String s) { List<List<String>> res = new ArrayList<>(); List<String> list = new ArrayList<>(); helper(res, list, 0, s); return res; } private void helper(List<List<String>> res, List<String> list, int level, String s) { if (level == s.length()) { res.add(new ArrayList<>(list)); return; } for (int i = level; i < s.length(); i++) { if (isPalin(s, level, i)) { list.add(s.substring(level, i + 1)); helper(res, list, i + 1, s); list.remove(list.size() - 1); } } } private boolean isPalin(String s, int start, int end) { while (start < end) { if (s.charAt(start) != s.charAt(end)) { return false; } start += 1; end -= 1; } return true; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步