# Palindrome string in algorithm

## 125 validate palindrome string

Given a string, verify whether it is a palindrome string. Only alphanumeric characters are considered, and the case of letters can be ignored.

Enter a string and output a Boolean value indicating whether the string is a palindrome string

Input: "A man, a plan, a canal: Panama"
Output: true
Explanation: "Amana planacanalpanama" is a palindrome string

Resolution:

Palindrome strings can generally be verified from two ends to the middle by using double pointers, or whether they are palindrome strings can be verified from the middle to both ends by using the central expansion method.

This question can adopt the strategy of double pointer to test character by character from the beginning and end of the string. However, the string given in this question has many non alphabetic characters. You can use a new string to save all the lowercase forms of letters in the original string, and then use the double pointer method to verify it.

```class Solution {
public:
bool isPalindrome(string s) {
// Filter out letter content
string sgood;
for (char ch: s) {
if (isalnum(ch)) {
sgood += tolower(ch);
}
}
// Double pointer validation palindrome string
int n = sgood.size();
int left = 0, right = n - 1;
while (left < right) {
if (sgood[left] != sgood[right]) {
return false;
}
++left;
--right;
}
return true;
}
};
```

## 680 validate palindrome string II

Given a non empty string s, at most one character can be deleted. Determine whether it can be a palindrome string.

Enter a string and output a Boolean value indicating whether the string can form a palindrome string through deletion

Enter: s = "abca"
Output: true
Explanation: you can delete the c character.

Resolution:

This problem uses the double pointer method to test the palindrome string. The two pointers point to the beginning and end of the string respectively, and compare the elements pointed to by the two pointers one by one. When the characters pointed to by the two pointers do not want to wait, check whether the palindrome string can still be formed by continuing to compare the characters under the two conditions of deleting the element pointed to by the head pointer and deleting the element pointed to by the tail pointer.

```class Solution {
public:
bool subPalindrome(string s, int head, int tail){
return false;
}
}
return true;
}

bool validPalindrome(string s) {
int head = 0, tail = s.size()-1;
--tail;
}else{
return true;
}else{
return false;
}
}
}
return true;
}
};
```

## 647 palindrome substring

Given a string, find how many palindrome substrings it has. Palindrome is defined as left-right symmetry.

The input is a string and the output is an integer representing the number of palindrome substrings.

Input: s = "aaa"
Output: 6
Explanation: 6 palindrome substrings: "a", "a", "a", "aa", "aa", "aaa"

Resolution:

This topic adopts the center expansion method, which can start from each position of the string and extend to the left and right to judge how many palindrome substrings exist with the current position as the central axis. Center expansion method: enumerate every possible palindrome center, and then expand to the left and right sides with two pointers. Expand when the elements pointed to by the two pointers are the same, otherwise stop expanding. In the enumeration process, the length of the molecular string to be distinguished is even and odd.

```class Solution {
public:
int countCurSub(string s, int lsh, int rsh){
int count = 0;
while(lsh>=0 && rsh<s.length() && s[lsh]==s[rsh]){
--lsh;
++rsh;
++count;
}
return count;
}

int countSubstrings(string s) {
int ans = 0;
for(int i=0;i<s.length();++i){
// Palindrome substring with odd length
ans += countCurSub(s,i,i);
// Palindrome substring with even length
ans += countCurSub(s,i,i+1);
}
return ans;
}
};
```

## 409 longest palindrome string

Given a string containing uppercase and lowercase letters, find the longest palindrome string constructed from these letters.

Enter a string and output an integer to represent the length of the longest palindrome string that can be composed

Input: "abccdd"
Output: 7
Explanation: the longest palindrome string that can be constructed is "dccaccd", and its length is 7

Resolution:

This topic uses hash table to assist in statistics to give the frequency of each character in the string. All characters with even frequency can be added to form a palindrome string, and those with odd frequency minus one can also be added to form a palindrome string. It should be noted that if there are characters with odd frequency, the total length of the palindrome string can be odd, and this character can appear in the center of the palindrome string.

```class Solution {
public:
int longestPalindrome(string s) {
unordered_map<char,int> chCnt;
for(const auto ch: s){
++chCnt[ch];
}
bool hasOdd = false;
int ans = 0;
for(const auto [key,cnt]: chCnt){
if(cnt&1){
ans+=(cnt-1);
hasOdd = true;
}else{
ans+=cnt;
}
}
return hasOdd?ans+1:ans;
}
};
```

## 5 longest palindrome substring

Given a string s, find the longest palindrome substring in S.

Enter a string and output a string representing the longest palindrome substring

Output: "bab"
Explanation: "aba" is also the answer to the question

Resolution:

This topic and 647 palindrome substring Similarly, the center expansion method can be used to find the longest palindrome substring. When the length of the substring is odd and even, there is only one center of the odd number, and the centers of the even length are i and i+1; In this center, use two pointers to expand left and right respectively, and compare whether the values of the two pointers pointing to the elements are equal one by one; In case of inequality, a pair object is used to return the start position and end position of the verified palindrome substring.

Traverse all palindrome substrings, compare the length of the substring, constantly update the start and end positions of the longer palindrome substring, and finally slice the longest palindrome substring using the substr() method.

```class Solution {
public:
pair<int,int> subStr(string s, int lsh, int rsh){
while(lsh>=0 && rsh<s.length() &&s[lsh]==s[rsh]){
--lsh;
++rsh;
}
return {lsh+1,rsh-1};
}

string longestPalindrome(string s) {
int start = 0, end=0;
for(int i=0; i<s.length();++i){
pair<int,int> oddStr = subStr(s,i,i);
pair<int,int> evenStr = subStr(s,i,i+1);
if(oddStr.second-oddStr.first > end-start){
start = oddStr.first;
end = oddStr.second;
}
if(evenStr.second-evenStr.first > end-start){
start = evenStr.first;
end = evenStr.second;
}
}
return s.substr(start,end-start+1);
}
};
```

## reference material

LeetCode 101: easily brush questions with you (C + +) Chapter 12 heady strings

Posted on Wed, 03 Nov 2021 21:47:18 -0400 by Kilo