19ms Swift solution


  • 0
    D

    it‘s a law in the string , you just to caculate the total same letters

     func longestPalindrome(_ s: String) -> Int {
              var res = 0
        
        var letters = Array(repeating: 0, count: 200)
        
        let strArray = s.cString(using: String.Encoding.ascii)
        
        for temp in strArray! {
            
            if letters[temp.hashValue] == 0 {
                letters[temp.hashValue] = 1
            }else{
                letters[temp.hashValue] += 1
            }
            print(temp.hashValue)
        }
        
        for temp in letters {
            if temp > 1 {
                temp % 2 == 0 ? (res += temp) : (res += (temp-1))
            }
        }
        
        if res == s.characters.count {
            return res
        }
        return res + 1;
        }
    

Log in to reply
 

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