---
# System prepended metadata

title: LeetCode 125. Valid Palindrome
tags: [Leetcode]

---

# LeetCode 125. Valid Palindrome
[LeetCode 125. Valid Palindrome](https://leetcode.com/problems/valid-palindrome/description/) (<font color=#00AF9B>Easy</font> 47.2%)

    
- 限制 :
    > 1 <= s.length <= 2 * $10^5$

    > s consists only of printable ASCII characters.


- Solution
    這題要花比較多的時間是在要知道如何使用 isdigit、isalpha、tolower，其他還好。
    - 時間複雜度: $O(n)$
    - 空間複雜度: $O(1)$

- 程式碼
    ```c++=
    class Solution {
    public:
        bool isPalindrome(string s) {
            for (int i = 0; i < s.size(); i++) 
            {
                if (isalpha(s[i]) != false || isdigit(s[i]) != false)
                {
                    int left_index = 0, right_index = s.size() - 1;
                    while (left_index <= right_index) 
                    {
                        while (left_index < s.size() && isalpha(s[left_index]) == false && isdigit(s[left_index]) == false)
                            left_index++;
                        while (right_index >= 0 && isalpha(s[right_index]) == false && isdigit(s[right_index]) == false)
                            right_index--;
                        if (tolower(s[left_index]) != tolower(s[right_index]))
                            return false;
                        left_index++;
                        right_index--;
                    }
                    break;
                }
            }
            return true;
        }
    };
    ```
</details>