> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/01-31-2022-125.md).

# 01/31/2022 125

Method1: create a stringBuilder object, iterate the String, only append the alphanumeric character and also convert it to lower case letter. Then return the value of comparing the string with the reversed string.&#x20;

Time O(n)

Space O(n)

```
class Solution { 
    public boolean isPalindrome(String s) { 
    StringBuilder st = new StringBuilder(); 
    for(char ch: s.toCharArray()){ 
    if(Character.isLetterOrDigit(ch)){ 
    st.append(Character.toLowerCase(ch)); } }
    String filtered = st.toString();
    String reversed = st.reverse().toString();
    
    return filtered.equals(reversed);
}
```

}

Method 2:

Use two pointers. One is from the left and another is from the right. Skip the non-alphanumeric characters and compare them iteratively.&#x20;

Time O(N)

Space O(1)

(use while loop and remember the conditions check: left < right && Character.isLetterOrDigit())&#x20;

```
class Solution { 
    public boolean isPalindrome(String s) { 
    s = s.toLowerCase();// remember
    int left = 0;
    int right = s.length() - 1;
    while(left < right){
        while (left < right && !Character.isLetterOrDigit(s.charAt(left))) left++;// need to remebeer
        while (left < right && !Character.isLetterOrDigit(s.charAt(right))) right--;
        if(s.charAt(left) != s.charAt(right)){
            return false;
        }
        left++;
        right--;         
    }
    return true;
}
```

}
