您的位置:首页 > 其它

LeetCode - Palindrome Partitioning II

2014-02-26 23:22 323 查看
Palindrome Partitioning II

2014.2.26 22:57

Given a string s, partition s such that every substring of the partition is a palindrome.

Return the minimum cuts needed for a palindrome partitioning of s.

For example, given s =
"aab"
,
Return
1
since the palindrome partitioning
["aa","b"]
could be produced using 1 cut.

Solution:

  This problem can be solved with dynamic programming. First check if every segment is palindromic, then do the DP.

  The idea is explained in the code comment, please see for yourself.

  Total time and space complexities are both O(n^2).

Accepted code:

// 1WA, 1AC, O(n^2) solution with DP
#include <string>
using namespace std;

class Solution {
public:
int minCut(string s) {
int **pal = nullptr;
int *dp = nullptr;
int len = (int)s.size();

if (len <= 1) {
return 0;
}

pal = new int*[len];
dp = new int[len + 1];

int i, j;
for (i = 0; i < len; ++i) {
pal[i] = new int[len];
}
for (i = 0; i < len; ++i) {
for (j = 0; j < len; ++j) {
pal[i][j] = 0;
}
}

// pal[i][j] means whether the substring s[i:j] is a palindrome.
for (i = 0; i < len; ++i) {
pal[i][i] = 1;
}
for (i = 0; i < len - 1; ++i) {
pal[i][i + 1] = (s[i] == s[i + 1]) ? 1 : 0;
}
for (i = 2; i <= len - 1; ++i) {
for (j = 0; j + i < len; ++j) {
pal[j][j + i] = (pal[j + 1][j + i - 1] && (s[j] == s[j + i])) ? 1 : 0;
}
}

// dp[i] means the minimal number of segments the substring s[0:i]
// must be cut, so that they're all palindromes.
dp[0] = 0;
for (i = 1; i <= len; ++i) {
dp[i] = i;
for (j = 0; j < i; ++j) {
if (pal[j][i - 1]) {
dp[i] = mymin(dp[j] + 1, dp[i]);
}
}
}

int ans = dp[len];
for (i = 0; i < len; ++i) {
delete[] pal[i];
}
delete[] pal;
delete[] dp;

return ans - 1;
}
private:
int mymin(const int x, const int y) {
return (x < y ? x : y);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: