Skip to content

Commit c0ac862

Browse files
committed
solved: 383. Ransom Note
Signed-off-by: rajput-hemant <[email protected]>
1 parent 20deb14 commit c0ac862

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

0 commit comments

Comments
 (0)