[LeetCode] Valid Palindrome IV

2330. Valid Palindrome IV

You are given a 0-indexed string s consisting of only lowercase English letters. In one operation, you can change any character of s to any other character.

Return true if you can make s a palindrome after performing exactly one or two operations, or return false otherwise.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
bool makePalindrome(string s) {
int i = 0, j = s.length() - 1, change = 0;
while(i <= j) {
if(s[i++] != s[j--]) change++;
}
return change <= 2;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/10/PS/LeetCode/valid-palindrome-iv/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.