Created
August 10, 2017 13:43
-
-
Save luojiyin1987/e73b3a7fda9857705c6c98076b823fa8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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