383. 赎金信

题目链接:https://leetcode-cn.com/problems/ransom-note/
执行用时:36 ms, 在所有 Swift 提交中击败了97.22%的用户
内存消耗:13.9 MB, 在所有 Swift 提交中击败了86.11%的用户

解题思路

和242一样的解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool {
if ransomNote.count > magazine.count {
return false
}
var table = [Int].init(repeating: 0, count: 26)
let chValue = Int("a".unicodeScalars.first!.value)
for item in magazine.unicodeScalars {
table[Int(item.value) - chValue] += 1
}
for i in ransomNote.unicodeScalars {
table[Int(i.value) - chValue] -= 1
}
guard table.first(where: {$0 < 0}) != nil else{
return true
}
return false
}
}

383. 赎金信
https://pisces34.github.io/2021/09/26/leetcode/383/
发布于
2021年9月26日
许可协议