★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝()➤GitHub地址:➤原文地址: ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
This is case sensitive, for example "Aa"
is not considered a palindrome here.
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.
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。
在构造过程中,请注意区分大小写。比如 "Aa"
不能当做一个回文字符串。
注意:
假设字符串的长度不会超过 1010。示例 1:
输入:"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 = 011 //遍历数组12 for i in 0..<12813 {14 ans += arr[i] / 2 * 215 if ans % 2 == 0 && arr[i] % 2 == 116 {17 ans += 118 }19 }20 return ans21 }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 }
28ms
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 += 210 }11 }12 13 if length < s.count {14 length += 115 }16 17 return length18 }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 = true10 var cSet = [Character : Int]()11 s.forEach {12 cSet[$0, default: 0] += 113 }14 for (_, n) in cSet {15 if (n & 1) == 0 {16 pSum += n17 } else {18 pSum += n - 119 if (firstSwitch) {20 pSum += 121 firstSwitch = false22 }23 }24 }25 return pSum26 }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] = 113 } else {14 count += value15 frequencyTable[key] = 016 }17 }18 19 for (_,value) in frequencyTable {20 if value%2 == 1 {21 count += 122 break23 }24 }25 26 return count27 }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 110 if s.count == 1 {11 return 112 }13 14 // there can be 1 odd character included15 // the rest can only be included in multiples of 216 // create a hashmap to keep track of the number of occurances17 var characters = [Character: Int]()18 for char in s {19 if let count = characters[char] {20 characters[char] = count + 121 } else {22 characters[char] = 123 }24 }25 26 // if there's a 1 or modulo > 0, set a variable to 127 // loop through the characters and divide by 228 for key in characters.keys {29 if characters[key]! % 2 != 0 {30 singleChar = 131 }32 33 longestPalindrome += characters[key]! / 2 * 234 // divide by 2 to eliminate remainders35 }36 37 longestPalindrome += singleChar38 39 return longestPalindrome40 }41 }