给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明:本题中,我们将空字符串定义为有效的回文串。
示例 1:
输入: "A man, a plan, a canal: Panama"
输出: true
C++ 双指针:
class Solution {
public:
bool isPalindrome(string s) {
string s_z;
for(char c:s){
if(isalnum(c)) s_z += tolower(c);
}
int i = 0, j = s_z.size()-1;
while(i<j){
if(s_z[i] != s_z[j]) return false;
i++;j--;
}
return true;
}
};
class Solution {
public:
bool isPalindrome(string s) {
string s_z;
for(char c:s){
if(isalnum(c)) s_z += tolower(c);
}
string s_f(s_z.rbegin(), s_z.rend());
return s_z==s_f;
}
};
python:
class Solution:
def isPalindrome(self, s: str) -> bool:
res = ''
for c in s:
if c.isalnum():
res += c.lower()
i = 0
j = len(res)-1
while i<j:
if res[i] != res[j]:
return False
i += 1
j -= 1
return True
class Solution:
def isPalindrome(self, s: str) -> bool:
sgood = "".join(ch.lower() for ch in s if ch.isalnum())
return sgood == sgood[::-1]
给你一个字符串 s
,找到 s
中最长的回文子串。
输入:s = "babad"
输出:"bab"
解释:"aba" 同样是符合题意的答案。
C++动态规划:
class Solution {
public:
string longestPalindrome(string s) {
int n = s.size();
if(n<2) return s;
vector<vector<bool>> dp(n,vector<bool>(n));
int start = 0, max_len = 1;
for(int j=1;j<n;j++){
for(int i=0;i<j;i++){
if(j-i<=2){ //判断中间三个是否是回文的
if(s[i]==s[j]) dp[i][j] = true;
}
else{
if(s[i]==s[j] && dp[i+1][j-1]) dp[i][j] = true; //当子串长度大于2时,判断i和j中间的字符串是否为回文的
}
if(dp[i][j]){
if(max_len<(j-i+1)){
max_len = j-i+1;
start = i;
}
}
}
}
return s.substr(start, max_len);
}
};
Python动态规划:
class Solution:
def longestPalindrome(self, s: str) -> str:
n = len(s)
if n == 1:
return s
dp = [[False for _ in range(n)] for _ in range(n)]
# dp = [[False] * n] * n //这样写有问题,*n相当于复制了n次列表,当修改其中一个列表时其他的也会被修改
max_len = 1
start = 0
for j in range(1,n):
for i in range(j):
if j-i<=2:
if s[i]==s[j]:
dp[i][j] = True
else:
if s[i]==s[j] and dp[i+1][j-1]:
dp[i][j] = True
if dp[i][j]:
if max_len < (j-i+1):
max_len = j-i+1
start = i
return s[start:start+max_len]
给定一个字符串 s
,找到其中最长的回文子序列,并返回该序列的长度。可以假设 s
的最大长度为 1000
。
示例 1:
输入:
"bbbab"
输出:
4
一个可能的最长回文子序列为 "bbbb"。
C++:
class Solution {
public:
int longestPalindromeSubseq(string s) {
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n, 0));
for(int i=n-1;i>=0;i--){
dp[i][i] = 1;
for(int j=i+1;j<n;j++){
if(s[i]==s[j]) dp[i][j] = dp[i+1][j-1] + 2;
else dp[i][j] = max(dp[i][j-1], dp[i+1][j]);
}
}
return dp[0][n-1];
}
};
python:
class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
n = len(s)
dp = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n-1, -1, -1):
dp[i][i] = 1
for j in range(i+1, n):
if s[i]==s[j]:
dp[i][j] = dp[i+1][j-1] + 2
else:
dp[i][j] = max(dp[i][j-1], dp[i+1][j])
return dp[0][n-1]
C++:
class Solution {
public:
int longestPalindrome(string word1, string word2) {
string word = word1 + word2;
int n = word.size();
int res = 0;
vector<vector<int>> dp(n, vector<int>(n, 0));
for(int i=n-1;i>=0;i--){
dp[i][i] = 1;
for(int j=i+1;j<n;j++){
if(word[i] == word[j]){
dp[i][j] = dp[i+1][j-1] + 2;
if(i<word1.size() && j>=word1.size()){
res = max(res, dp[i][j]);
}
}
else dp[i][j] = max(dp[i+1][j], dp[i][j-1]);
}
}
return res;
}
};
python:
class Solution:
def longestPalindrome(self, word1: str, word2: str) -> int:
word = word1 + word2
n = len(word)
res = 0
dp = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n-1, -1,-1):
dp[i][i] = 1
for j in range(i+1, n):
if word[i] == word[j]:
dp[i][j] = dp[i+1][j-1] + 2
if i<len(word1) and j >= len(word1):
res = max(res, dp[i][j])
else:
dp[i][j] = max(dp[i+1][j], dp[i][j-1])
return res