Post

Leetcode - 125. Valid Palindrome

Leetcode - 125. Valid Palindrome

Hits

  • A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.

    Given a string s, return true if it is a palindrome, or false otherwise.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
func isPalindrome(s string) bool {
    firstInd, lastInd := 0, len(s)-1

    for firstInd<lastInd{
        for firstInd<lastInd && !isLetterOrDigit(rune(s[firstInd])){
            firstInd++
        }
        for firstInd<lastInd && !isLetterOrDigit(rune(s[lastInd])){
            lastInd--
        }

        if strings.ToLower(string(s[firstInd])) != strings.ToLower(string(s[lastInd])){
            return false
        }
        firstInd++
        lastInd--
    }
    return true
    
}


func isLetterOrDigit(r rune)bool{
    return unicode.IsLetter(r) || unicode.IsDigit(r)
}
This post is licensed under CC BY 4.0 by the author.