-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path383_easy_RansomNote.go
More file actions
40 lines (32 loc) · 922 Bytes
/
Copy path383_easy_RansomNote.go
File metadata and controls
40 lines (32 loc) · 922 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
// ref: https://leetcode.com/problems/ransom-note/description/
func canConstruct(ransomNote string, magazine string) bool {
magzChar := make(map[rune]int)
for _, char := range magazine {
magzChar[char]++
}
for _, char := range ransomNote {
if magzChar[char] == 0 {
return false
}
magzChar[char]--
}
return true
}
// nice solution
// just init 26 arr (because of total alphabet)
// and then count total letter in magazines, and substract in every letter in ransomNote
// if less then 0 (means not enought letter) then return false.
// func canConstruct(ransomNote string, magazine string) bool {
// magazin := make([]int, 26)
// for _, v := range magazine {
// magazin[v - 'a']++
// }
// for _, v := range ransomNote {
// magazin[v - 'a']--
// if magazin[v - 'a' ] < 0 {
// return false
// }
// }
// return true
// }