给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以,返回 true ;否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次。
func canConstruct(ransomNote string, magazine string) bool {
char2cnt := map[uint8]uint32{}
for i := range magazine {
char2cnt[magazine[i]] ++
}
for i:= range ransomNote {
val, ok := char2cnt[ransomNote[i]]
if !ok {
return false
}
if val == 0 {
return false
}
char2cnt[ransomNote[i]] --
}
return true
}