Skip to content

Commit 93e49c4

Browse files
author
applewjg
committed
Update
1 parent 8c35a9f commit 93e49c4

File tree

2 files changed

+64
-41
lines changed

2 files changed

+64
-41
lines changed

PalindromePartitioning.h

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
2-
Author: Annie Kim, [email protected]
2+
Author: Annie Kim, [email protected] : King, [email protected]
33
Date: May 20, 2013
4+
Update: Oct 06, 2014
45
Problem: Palindrome Partitioning
56
Difficulty: Easy
67
Source: http://leetcode.com/onlinejudge#question_131
@@ -19,36 +20,60 @@
1920

2021
class Solution {
2122
public:
22-
vector<vector<string>> res;
2323
vector<vector<string>> partition(string s) {
24-
res.clear();
25-
vector<string> part;
26-
partitionRe(s, 0, part);
27-
return res;
24+
return partition_2(s);
2825
}
29-
30-
void partitionRe(const string &s, int start, vector<string> &part) {
31-
if (start == s.size())
32-
{
33-
res.push_back(part);
34-
return;
26+
vector<vector<string>> partition_2(string s) {
27+
int size = s.size();
28+
vector<vector<bool> > dp(size, vector<bool>(size));
29+
for (int i = size - 1; i >= 0; --i) {
30+
for (int j = i; j < size; ++j) {
31+
dp[i][j]=(s[i]==s[j])&&(j<i+2||dp[i+1][j-1]);
32+
}
3533
}
36-
string palindrom;
37-
for (int i = start; i < s.size(); ++i) {
38-
palindrom.push_back(s[i]);
39-
if (!isPalindrome(palindrom)) continue;
40-
part.push_back(palindrom);
41-
partitionRe(s, i + 1, part);
42-
part.pop_back();
34+
vector<vector<string> > res[size];
35+
for (int i = size - 1; i >= 0; --i) {
36+
for (int j = i; j < size; ++j) {
37+
if (dp[i][j] == false) continue;
38+
string word = s.substr(i, j - i + 1);
39+
if (j == size - 1) {
40+
res[i].push_back(vector<string>{word});
41+
} else {
42+
for (auto iter : res[j+1]) {
43+
iter.insert(iter.begin(), word);
44+
res[i].push_back(iter);
45+
}
46+
}
47+
}
4348
}
49+
return res[0];
4450
}
4551

46-
bool isPalindrome(const string &s) {
47-
int i = 0, j = s.size()-1;
48-
while (i < j) {
49-
if (s[i] != s[j]) return false;
50-
i++; j--;
52+
vector<vector<string>> partition_1(string s) {
53+
int size = s.size();
54+
vector<vector<bool> > dp(size, vector<bool>(size));
55+
for (int i = size - 1; i >= 0; --i) {
56+
for (int j = i; j < size; ++j) {
57+
dp[i][j]=(s[i]==s[j])&&(j<i+2||dp[i+1][j-1]);
58+
}
59+
}
60+
vector<vector<string> > res;
61+
vector<string> path;
62+
dfs(s, dp, 0, path, res);
63+
return res;
64+
}
65+
void dfs(string s, vector<vector<bool> > &dp, int start, vector<string> &path, vector<vector<string> > &res) {
66+
int size = s.size();
67+
if (start == size) {
68+
res.push_back(path);
69+
}
70+
for (int i = start; i < size; ++i) {
71+
if (dp[start][i] == false) {
72+
continue;
73+
}
74+
path.push_back(s.substr(start, i - start + 1));
75+
dfs(s, dp, i + 1, path, res);
76+
path.pop_back();
5177
}
52-
return true;
5378
}
5479
};

PalindromePartitioningII.h

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,20 @@
1717
class Solution {
1818
public:
1919
int minCut(string s) {
20-
int N = s.size();
21-
bool isP[N];
22-
int dp[N];
23-
dp[0] = 0;
24-
for (int i = 1; i < N; ++i)
25-
{
26-
isP[i] = true;
27-
dp[i] = dp[i-1] + 1;
28-
for (int j = 0; j < i; ++j)
29-
{
30-
isP[j] = (s[i] == s[j]) ? isP[j+1] : false; // isP[j] == true -> [j...i] is a palindrome
31-
// isP[j+1] == true -> [j+1...i-1] is a palindrome
32-
if (isP[j])
33-
dp[i] = (j == 0) ? 0 : min(dp[i], dp[j-1] + 1); // dp[i] -> minCount for [0...i]
20+
int size = s.size();
21+
vector<int> dp(size + 1);
22+
vector<bool> isP(size, true);
23+
dp[size] = -1;
24+
for (int i = size -1; i >= 0; --i) {
25+
dp[i] = dp[i + 1] + 1;
26+
for (int j = size - 1; j >= i; --j) {
27+
isP[j] = false;
28+
if (s[i] == s[j] && ( j - i < 2 || isP[j-1])) {
29+
isP[j] = true;
30+
dp[i] = min(dp[i], dp[j + 1] + 1);
31+
}
3432
}
3533
}
36-
return dp[N-1];
34+
return dp[0];
3735
}
38-
};
36+
};

0 commit comments

Comments
 (0)