- Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string.
The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.
Example
Example1
Input: “abc”
Output: 3
Explanation:
3 palindromic strings: “a”, “b”, “c”.
Example2
Input: “aba”
Output: 4
Explanation:
4 palindromic strings: “a”, “b”, “a”, “aba”.
Notice
The input string length won’t exceed 1000
解法1:暴力法。时间复杂度O(n^3)。
代码如下:
class Solution {
public:
/**
* @param str: s string
* @return: return an integer, denote the number of the palindromic substrings
*/
int countPalindromicSubstrings(string &str) {
int n = str.size();
if (n <= 1) return n;
int count = 0;
for (int i = 0; i < n; ++i) {
for (int j = i; j < n; ++j) {
string substring = str.substr(i, j - i + 1);
if (isPalindrome(substring)) count++;
}
}
return count;
}
private:
bool isPalindrome(string & str) {
int n = str.size();
if (n <= 1) return true;
int i = 0, j = n - 1;
while(i < j) {
if (str[i] != str[j]) return false;
i++;
j--;
}
return true;
}
};
解法2:O(n^2)。即算回文的同时count++。
注意用这种方法的时候要注意从i:i开始和i:i+1开始是不一样的,前者算的是类似(3:3, 2:4, 1:5),后者算的是(3:4, 2:5, 1:6),互不重合。
代码如下:
class Solution {
public:
/**
* @param str: s string
* @return: return an integer, denote the number of the palindromic substrings
*/
int countPalindromicSubstrings(string &str) {
int n = str.size();
if (n <= 1) return n;
int count = 0;
for (int i = 0; i < n; ++i) {
calPalindrom(str, i, i, count);
calPalindrom(str, i, i + 1, count);
}
return count;
}
private:
void calPalindrom(string & str, int start, int end, int & count) {
int n = str.size();
while(start >= 0 && end < n) {
if (str[start] == str[end]) {
count++;
start--;
end++;
} else {
break;
}
}
}
};
解法3:DP。时间复杂度O(n^2)。
dp[i][j]表示str(i…j)是否为回文串。
这里要非常注意i和j的递增方向。因为dp[i][j]取决于dp[i+1][j-1]是否为回文,即i应该递减,j应该递增。所以i是n-1->0,j是i->n-1。
另外,为什么dp[i+1][j-1]不会出现越界呢?即为什么永远i+1<=n-1,j-1>=0呢?
因为首先str[i]==str[j],这里i和j的范围都是0…n-1之间。另外j-i<=2,
如果i=n-1,因为j>=i,j一定为n-1,j-i<=2必定满足。
同样,如果j=0,因为j>=i,所以i一定为0,j-i<=2必定满足。
// i往左移动,j往右移动
// <-------i
// j-------->
class Solution {
public:
/**
* @param str: s string
* @return: return an integer, denote the number of the palindromic substrings
*/
int countPalindromicSubstrings(string &str) {
int n = str.size();
if (n <= 1) return n;
vector<vector<bool>> dp(n, vector<bool>(n, false));
int count = 0;
for (int i = n - 1; i >= 0; --i) {
for (int j = i; j < n; ++j) {
if (str[i] == str[j]) {
if (j - i <= 2 || dp[i + 1][j - 1]) {
dp[i][j] = true;
count++;
}
}
}
}
return count;
}
};
二刷: i和j移动互换方向
// i往右移动,j往左移动
// i---------->
// <-------j
class Solution {
public:
/**
* @param str: s string
* @return: return an integer, denote the number of the palindromic substrings
*/
int countPalindromicSubstrings(string &str) {
int n = str.size();
if (n <= 1) return n;
vector<vector<bool>> dp(n, vector<bool>(n, false));
int count = 0;
for (int i = 0; i < n; ++i) {
for (int j = i; j >= 0; --j) {
if (str[i] == str[j]) {
if (i - j <= 2 || dp[j + 1][i - 1]) {
dp[j][i] = true;
count++;
}
}
}
}
return count;
}
};
重刷DP:没有上面的简洁。
class Solution {
public:
/**
* @param str: s string
* @return: return an integer, denote the number of the palindromic substrings
*/
int countPalindromicSubstrings(string &str) {
int n = str.size();
vector<vector<int>> dp(n, vector<int>(n, 0));
int count = 0;
for (int i = 0; i < n; i++) {
dp[i][i] = 1;
}
for (int i = 1; i < n; i++) {
if (str[i] == str[i - 1]) dp[i - 1][i] = 1;
}
for (int i = 0; i < n; i++) {
for (int j = 1; j < n; j++) {
if (i - j >= 0 && i + j < n) {
if (str[i - j] == str[i + j]) {
if (dp[i - j + 1][i + j - 1]) dp[i - j][i + j] = 1;
}
}
if (i - j >= 0 && i + j + 1 < n) {
if (str[i - j] == str[i + j + 1]) {
if (dp[i - j + 1][i + j]) dp[i - j][i + j + 1] = 1;
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dp[i][j]) count++;
}
}
return count;
}
};