# 125. Valid Palindrome

## 1.問題&#x20;

* 判斷一個字串是否屬於回文, 只需要考慮數字及字母, 字母可忽略大小寫

![](/files/-LNPBllwOauFIOaaTliT)

## 2.想法 <a href="#id-2-xiang-fa" id="id-2-xiang-fa"></a>

* 提問
  * 確認題意: &#x20;
* function header, parameter
* test input
* 觀察
  * 保留字串中屬於字母及數字的部分, 並從兩側比對
* 說明想法
* 測試計算複雜度

## **3.程式碼** <a href="#id-3-cheng-shi" id="id-3-cheng-shi"></a>

```
class Solution {
public:
    bool isPalindrome(string s) {
        if (s.empty()) {
            return true;
        }
        
        string s2="";
        int size = s.length();
        for (int i = 0; i < size; i++) {
            if (isalpha(s[i]) || isdigit(s[i])) {
                s2.push_back(tolower(s[i]));
            }
        }
        
        for (int i = 0; i < s2.length()/2; i++) {
            if (s2[i] != s2[s2.length() - 1 -i]) {
                return false;
            }
        }
        
        return true;
    }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jenhsuan.gitbook.io/algorithm/leetcode/125.-valid-palindrome.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
