在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a string You can use each character in
Example 1: Input: text = "nlaebolko" Output: 1 Example 2: Input: text = "loonbalxballpoon" Output: 2 Example 3: Input: text = "leetcode" Output: 0
Constraints:
给你一个字符串 字符串
示例 1: 输入:text = "nlaebolko" 输出:1 示例 2: 输入:text = "loonbalxballpoon" 输出:2 示例 3: 输入:text = "leetcode" 输出:0
提示:
12ms
1 class Solution { 2 func maxNumberOfBalloons(_ text: String) -> Int { 3 var textCountMap: [Character: Int] = ["b":0, 4 "l":0, 5 "a":0, 6 "o":0, 7 "n":0] 8 for char in text { 9 switch char { 10 case "b", 11 "l", 12 "a", 13 "o", 14 "n": 15 textCountMap[char, default: 0] += 1 16 default: 17 continue 18 } 19 } 20 21 if textCountMap["o"]! % 2 == 1 { 22 textCountMap["o"] = textCountMap["o"]! - 1 23 } 24 25 if textCountMap["l"]! % 2 == 1 { 26 textCountMap["l"] = textCountMap["l"]! - 1 27 } 28 29 return min(textCountMap["b"]!, textCountMap["n"]!, textCountMap["a"]!, textCountMap["o"]!/2, textCountMap["l"]!/2) 30 } 31 } Runtime: 16 ms Memory Usage: 20.7 MB
1 class Solution { 2 func maxNumberOfBalloons(_ text: String) -> Int { 3 var F:[Character:Int] = [Character:Int]() 4 for c in text 5 { 6 F[c,default:0] += 1 7 } 8 return min(F["b",default:0], F["a",default:0], F["l",default:0]/2, F["o",default:0]/2, F["n",default:0]) 9 } 10 }
|
请发表评论