★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-xeragnvl-cs.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.git
This is case sensitive, for example "Aa"
is not considered a palindrome here.github
Note:
Assume the length of given string will not exceed 1,010.數組
Example:微信
Input: "abccccdd" Output: 7
Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
給定一個包含大寫字母和小寫字母的字符串,找到經過這些字母構形成的最長的迴文串。ide
在構造過程當中,請注意區分大小寫。好比 "Aa"
不能當作一個迴文字符串。oop
注意:
假設字符串的長度不會超過 1010。ui
示例 1:spa
輸入: "abccccdd" 輸出: 7 解釋: 咱們能夠構造的最長的迴文串是"dccaccd", 它的長度是 7。
20ms
1 class Solution { 2 func longestPalindrome(_ s: String) -> Int { 3 // 4 var arr:[Int] = Array(repeating: 0,count: 128) 5 //遍歷字符串 6 for char in s.characters 7 { 8 arr[char.toInt()] += 1 9 } 10 var ans:Int = 0 11 //遍歷數組 12 for i in 0..<128 13 { 14 ans += arr[i] / 2 * 2 15 if ans % 2 == 0 && arr[i] % 2 == 1 16 { 17 ans += 1 18 } 19 } 20 return ans 21 } 22 } 23 //Character擴展代碼 24 extension Character 25 { 26 func toInt() -> Int 27 { 28 var num:Int = Int() 29 for scalar in String(self).unicodeScalars 30 { 31 num = Int(scalar.value) 32 } 33 return num 34 } 35 }
28msscala
1 class Solution { 2 func longestPalindrome(_ s: String) -> Int { 3 var map: [Bool] = Array(repeating: false, count: 128) 4 var length = 0 5 for char in s.unicodeScalars { 6 let value = Int(char.value) 7 map[value] = !map[value] 8 if !map[value] { 9 length += 2 10 } 11 } 12 13 if length < s.count { 14 length += 1 15 } 16 17 return length 18 } 19 }
28ms
1 class Solution { 2 struct CharTracker { 3 let c: Character 4 let n: Int 5 } 6 func longestPalindrome(_ s: String) -> Int { 7 var charTracker: [CharTracker] = [CharTracker]() 8 var pSum = 0 9 var firstSwitch: Bool = true 10 var cSet = [Character : Int]() 11 s.forEach { 12 cSet[$0, default: 0] += 1 13 } 14 for (_, n) in cSet { 15 if (n & 1) == 0 { 16 pSum += n 17 } else { 18 pSum += n - 1 19 if (firstSwitch) { 20 pSum += 1 21 firstSwitch = false 22 } 23 } 24 } 25 return pSum 26 } 27 }
32ms
1 class Solution { 2 func longestPalindrome(_ s: String) -> Int { 3 var frequencyTable = [Character:Int]() 4 for ch in s { 5 frequencyTable[ch] = (frequencyTable[ch] ?? 0) + 1 6 } 7 8 var count = 0 9 for (key,value) in frequencyTable { 10 if value%2 == 1 { 11 count += (value-1) 12 frequencyTable[key] = 1 13 } else { 14 count += value 15 frequencyTable[key] = 0 16 } 17 } 18 19 for (_,value) in frequencyTable { 20 if value%2 == 1 { 21 count += 1 22 break 23 } 24 } 25 26 return count 27 } 28 }
36ms
1 class Solution { 2 func longestPalindrome(_ s: String) -> Int { 3 var longestPalindrome = 0 4 var singleChar = 0 5 6 // check that the string is not empty 7 guard s.count > 0 else { return 0 } 8 9 // if there is only 1 character, return 1 10 if s.count == 1 { 11 return 1 12 } 13 14 // there can be 1 odd character included 15 // the rest can only be included in multiples of 2 16 // create a hashmap to keep track of the number of occurances 17 var characters = [Character: Int]() 18 for char in s { 19 if let count = characters[char] { 20 characters[char] = count + 1 21 } else { 22 characters[char] = 1 23 } 24 } 25 26 // if there's a 1 or modulo > 0, set a variable to 1 27 // loop through the characters and divide by 2 28 for key in characters.keys { 29 if characters[key]! % 2 != 0 { 30 singleChar = 1 31 } 32 33 longestPalindrome += characters[key]! / 2 * 2 34 // divide by 2 to eliminate remainders 35 } 36 37 longestPalindrome += singleChar 38 39 return longestPalindrome 40 } 41 }