Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created August 10, 2017 13:43
Show Gist options
  • Save luojiyin1987/e73b3a7fda9857705c6c98076b823fa8 to your computer and use it in GitHub Desktop.
Save luojiyin1987/e73b3a7fda9857705c6c98076b823fa8 to your computer and use it in GitHub Desktop.
func canConstruct(ransomNote string, magazine string) bool {
m := [26]int{}
for _, v := range magazine {
m[v-'a']++
}
for _, v := range ransomNote {
if m[v-'a'] == 0 {
return false
}
m[v-'a']--
}
return true
}
//------------------------------------------------------------//
func canConstruct(ransomNote string, magazine string) bool {
temp := make(map[rune]int)
for _, v := range magazine {
_, ok := temp[v] ;
if ok {
temp[v] += 1
} else {
temp[v] =1
}
}
for _, v := range ransomNote {
_, ok := temp[v]
if ok {
temp[v] -=1
} else {
return false
}
}
for _, v := range temp {
if v <0 {
return false
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment