Cover image for Interview Classic 150 P125: Valid Palindrome

Interview Classic 150 P125: Valid Palindrome


Timeline

Timeline

2025-10-17

init

string

Problem:

Using two pointers would definitely be faster, but here I directly used the standard library’s reverse.

123456789101112131415161718192021222324252627282930313233
#include <algorithm>#include <cstring>#include <string>using std::string;class Solution {public:  bool isPalindrome(string s) {    int i;    int n = s.size();    string original;    for (i = 0; i < n; i++) {      if (s[i] >= 'A' && s[i] <= 'Z')        s[i] = s[i] - ('A' - 'a');            if (!(s[i] >= 'a' && s[i] <= 'z') && !(s[i] >= '0' && s[i] <= '9'))        s[i] = '*';    }    s.erase(std::remove(s.begin(), s.end(), '*'), s.end());    original = s;    std::reverse(s.begin(), s.end());    if (std::strcmp(s.c_str(), original.c_str()) == 0)      return true;    else      return false;    }};
Loading comments…