for Robot Artificial Inteligence

16. Valid Palindrome Solution

|

class Solution {
public:
    bool isPalindrome(string s) {
        for(int i=0, j=s.size() -1 ; i<j; i++, j--)
        {
            while(isalnum(s[i])==false && i<j) i++;
            while(isalnum(s[j])==false && i<j) j--;
            if(toupper(s[i])!=toupper(s[j])) return false;
        }
        return true;
    }
};
  • Move 2 pointers from each end until they collide
  • Increment left pointer if not alphanumeric(숫자나 글자가 아닐경우)
  • decrement right pointer if not alphanumeric(숫자나 글자가 아닐경우)
  • Exit and return error if not match

Comments