思路
和求交集的题目有些类似,用map保存所有元素,遍历查找
code
func canConstruct(ransomNote string, magazine string) bool {
m := make(map[rune]int)
for _, v := range magazine {
m[v]++
}
for _, v := range ransomNote {
c, ok := m[v]
if ok == false || c == 0 {
return false
}
m[v]--
}
return true
}
更多内容请移步我的repo:https://github.com/anakin/golang-leetcode