详细见:leetcode.com/problems/palindrome-partitioning
Java Solution: github
package leetcode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/*
* 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"]
]
*/
/**
* @author zxwtry
* @email zxwtry@qq.com
* @project OJ
* @package leetcode
* @file P131_PalindromePartitioning.java
* @type P131_PalindromePartitioning
* @date 2016年12月13日 下午6:13:52
* @details Solution1: AC 10ms 50.34%
* @details Solution2: AC 10ms 50.34%
* @details Solution3: AC 8ms 79.05%
*/
public class P131_PalindromePartitioning {
static class Solution1 {
List<List<String>> ans = new LinkedList<List<String>>();
LinkedList<String> l = new LinkedList<String>();
char[] cs = null;
public List<List<String>> partition(String s) {
cs = s.toCharArray();
for (int i = 0; i < cs.length; i ++) {
search(0, i);
}
return ans;
}
private void search(int s, int t) {
if (isP(s, t)) {
l.add(new String(cs, s, t-s+1));
for (int nt = t+1; nt < cs.length; nt ++)
search(t+1, nt);
if (t == cs.length-1)
ans.add(new LinkedList<>(l));
l.removeLast();
}
}
private boolean isP(int s, int t) {
while (s < t) {
if (cs[s] != cs[t]) {
return false;
} else {
s ++;
t --;
}
}
return true;
}
}
static class Solution2 {
public List<List<String>> partition(String s) {
int sn = s == null ? 0 : s.length();
if (sn == 0) return new ArrayList<List<String>>(0);
List<List<List<String>>> rec = new ArrayList<>(sn+1);
for (int i = 0; i <= sn; i ++)
rec.add(new ArrayList<List<String>>());
rec.get(0).add(new ArrayList<String>());
boolean[][] pair = new boolean[sn][sn];
for (int i = 0; i < sn; i ++) {
for (int left = 0; left <= i; left ++) {
if (s.charAt(left) == s.charAt(i) && (i <= 1 + left || pair[left + 1][i - 1])) {
pair[left][i] = true;
String str = s.substring(left, i+1);
for (List<String> r : rec.get(left)) {
List<String> ri = new ArrayList<String>(r);
ri.add(str);
rec.get(i+1).add(ri);
}
}
}
}
return rec.get(sn);
}
}
static class Solution3 {
List<List<String>> ans = new LinkedList<List<String>>();
LinkedList<String> l = new LinkedList<String>();
char[] cs = null;
boolean[][] pal = null;
public List<List<String>> partition(String s) {
if (s == null) return ans;
cs = s.toCharArray();
pal = new boolean[cs.length][cs.length];
for (int i = 0; i < cs.length; i ++) {
search(0, i);
}
return ans;
}
private void search(int s, int t) {
if (cs[s] == cs[t] && (t <= 1+s || pal[s+1][t-1])) {
pal[s][t] = true;
l.add(new String(cs, s, t-s+1));
for (int nt = t+1; nt < cs.length; nt ++)
search(t+1, nt);
if (t == cs.length-1)
ans.add(new LinkedList<>(l));
l.removeLast();
}
}
}
}
C Solution: github
#pragma warning(disable:4786)
/*
url: leetcode.com/problems/palindrome-partitioning
AC 6ms 75.03%
*/
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>
using namespace std;
class Solution {
public:
int isP(string& s, int i, int j) {
while (i < j) {
if (s[i] != s[j]) return 0;
i ++;
j --;
}
return 1;
}
void search(vector<vector<string > > &ans, vector<string >& rec, string& s, int i, int j) {
if (isP(s, i, j)) {
rec.push_back(s.substr(i, j-i+1));
for (int nt = j+1; nt < s.size(); nt ++) {
search(ans, rec, s, j+1, nt);
}
if (j == s.size()-1) {
ans.push_back(rec);
}
rec.pop_back();
}
}
vector<vector<string > > partition(string s) {
vector<vector<string > > ans;
vector<string > rec;
for (int i = 0; i < s.size(); i ++)
search(ans, rec, s, 0, i);
return ans;
}
};
int main() {
string s = "aab";
vector<vector<string > > ans = Solution().partition(s);
for (int i = 0; i < ans.size(); i ++) {
cout<<"++++++++++++++++"<<endl;
for (int j = 0; j < ans[i].size(); j ++) {
cout<<ans[i][j]<<" ";
}
cout<<endl<<"----------------"<<endl;
}
return 0;
}
Python Solution: github
#coding=utf-8
'''
url: leetcode.com/problems/palindrome-partitioning
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年5月15日
@details: Solution: 162ms 77.62%
'''
class Solution(object):
def isP(self, s, i, j):
while i < j:
if s[i] != s[j]: return False
i, j = i + 1, j -1
return True
def search(self, s, sn, i, rec, ans):
if i == sn: ans.append(list(rec))
for j in range(i, sn):
if self.isP(s, i, j):
rec.append(s[i:j+1])
self.search(s, sn, j+1, rec, ans)
del rec[-1]
def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
rec = []
ans = []
sn = 0 if s == None else len(s)
self.search(s, sn, 0, rec, ans)
return ans
if __name__ == "__main__":
s = "aaab"
#print(s[0:1])
print(Solution().partition(s))