Swift 3 59ms 100% solution


  • 0
    Y
    func isPalindrome(_ s: String) -> Bool {
            if s == "" {
                return true
            }
            let str = s.lowercased()
            let arr = Array(str.utf16).map { Int($0) }
            var l = 0, r = arr.count - 1
            while (l < r) {
                while l < r && !check(arr[l]) { l += 1 }
                while l < r && !check(arr[r]) { r -= 1 }
                if arr[l] != arr[r] {
                    return false
                } else {
                    l += 1
                    r -= 1
                }
            }
            return true
        }
        
        func check(_ ch: Int) -> Bool {
            return (ch >= 97 && ch <= 122) || (ch >= 48 && ch <= 57)
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.