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.
For example, given s = "aab"
,
Return
[ ["aa","b"], ["a","a","b"] ]
class Solution: def partition(self, s: str) -> List[List[str]]: res = [] n = len(s) dp = [[0] * n for _ in range(n)] for i in range(n): dp[i][i] = 1 res = [] for i in range(n)[::-1]: for j in range(i,n): if s[i] == s[j]: if j-i<=2: dp[i][j] = 1 else: dp[i][j] = dp[i+1][j-1] def backtracking(index,path,dp): if index >= len(s): res.append(path.copy()) return for i in range(index,len(s)): if dp[index][i]: path.append(s[index:i+1]) backtracking(i+1,path,dp) path.pop() backtracking(0,[],dp) return res
class Solution { public: vector<vector<string>> res; vector<vector<string>> partition(string s) { int n = s.size(); vector<vector<bool>> dp = vector<vector<bool>>(n,vector<bool>(n,false)); for(int i = 0 ; i < n;i++) { dp[i][i] = true; } for(int end = 0; end < n ;end++) { for(int start = end -1; start >=0;start--) { if (s[start]==s[end]) { if (end-start<=1) { dp[start][end] = true; } else { dp[start][end] = dp[start+1][end-1]; } } } } vector<string> path; dfs(s,0,dp,path); return res; } bool isPalindrome(const string s,int start, int end) { for(int i = start,j = end; j>i;++i,--j) { if(s[i]!=s[j]) { return false; } } return true; } void dfs(string& s ,int start, vector<vector<bool>>&dp, vector<string>& path) { if (start == s.size()) { res.emplace_back(path); } for(int end = start; end < s.size();end++) { if(!dp[start][end]) continue; //if (!isPalindrome(s,start,end)) continue; path.emplace_back(s.substr(start,end-start+1)); dfs(s,end+1,dp,path); path.pop_back(); } } };
class Solution { public: vector<vector<string>> res; void backtrack(vector<string>& path,string& s, int start) { if (start == s.size()) { res.push_back(path); return; } for (int i = start; i < s.size(); ++i) { if (!isPalindrome(s,start,i)) { continue; } string ss = s.substr(start,i-start+1); path.push_back(ss); backtrack(path,s,i+1); path.pop_back(); } } bool isPalindrome(const string s,int start, int end) { for(int i = start,j = end; j>i;++i,--j) { if(s[i]!=s[j]) { return false; } } return true; } vector<vector<string>> partition(string s) { vector<string> path; backtrack(path,s,0); return res; } };
1 class Solution { 2 private List<List<String>> res = new ArrayList<>(); 3 public List<List<String>> partition(String s) { 4 5 help(new ArrayList<>(), s, 0); 6 return res; 7 } 8 9 private void help( List<String> temp, String s, int index){ 10 if(index == s.length()) 11 res.add(new ArrayList<>(temp)); 12 else{ 13 for(int i = index; i < s.length(); i++){ 14 if(isPalindrome(s, index, i)){ 15 temp.add(s.substring(index, i + 1)); 16 help(temp, s, i + 1); 17 temp.remove(temp.size() - 1); 18 } 19 } 20 } 21 } 22 23 public boolean isPalindrome(String s, int low, int high){ 24 while(low < high) 25 if(s.charAt(low++) != s.charAt(high--)) return false; 26 return true; 27 } 28 }