教日月换新天。为有牺牲多壮志,敢

[Swift]LeetCode409. 最长回文串 | Longest Palindrome

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9782912.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 }
复制代码

 

posted @   为敢技术  阅读(260)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°