[Swift]LeetCode409. 最长回文串 | Longest Palindrome
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9782912.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.
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 = 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 }
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 += 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 }