File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
src/0301-0400/383 - Ransom Note Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ func canConstruct (ransomNote string , magazine string ) bool {
4
+ if len (ransomNote ) > len (magazine ) {
5
+ return false
6
+ }
7
+ // make a map of the magazine
8
+ mag := make (map [rune ]int )
9
+
10
+ // count the number of each character in the magazine
11
+ for _ , c := range magazine {
12
+ mag [c ]++
13
+ }
14
+
15
+ // check if the ransom note can be made from the magazine
16
+ for _ , c := range ransomNote {
17
+ if mag [c ] == 0 {
18
+ return false
19
+ }
20
+ mag [c ]--
21
+ }
22
+
23
+ // // make a vector of the magazine
24
+ // magVec := make([]int, 26)
25
+
26
+ // for _, c := range magazine {
27
+ // magVec[c-'a']++
28
+ // }
29
+
30
+ // // check if ransomNote can be constructed from magazine
31
+ // for _, c := range ransomNote {
32
+ // if magVec[c-'a'] == 0 {
33
+ // return false
34
+ // }
35
+ // magVec[c-'a']--
36
+ // }
37
+
38
+ return true
39
+ }
You can’t perform that action at this time.
0 commit comments