-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path383.ransom-note.go
More file actions
84 lines (75 loc) · 1.57 KB
/
383.ransom-note.go
File metadata and controls
84 lines (75 loc) · 1.57 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* @lc app=leetcode id=383 lang=golang
*
* [383] Ransom Note
*
* https://leetcode.com/problems/ransom-note/description/
*
* algorithms
* Easy (54.94%)
* Likes: 1434
* Dislikes: 279
* Total Accepted: 328.3K
* Total Submissions: 595K
* Testcase Example: '"a"\n"b"'
*
* Given two stings ransomNote and magazine, return true if ransomNote can be
* constructed from magazine and false otherwise.
*
* Each letter in magazine can only be used once in ransomNote.
*
*
* Example 1:
* Input: ransomNote = "a", magazine = "b"
* Output: false
* Example 2:
* Input: ransomNote = "aa", magazine = "ab"
* Output: false
* Example 3:
* Input: ransomNote = "aa", magazine = "aab"
* Output: true
*
*
* Constraints:
*
*
* 1 <= ransomNote.length, magazine.length <= 10^5
* ransomNote and magazine consist of lowercase English letters.
*
*
*/
// @lc code=start
func canConstruct(ransomNote string, magazine string) bool {
return solution2(ransomNote, magazine)
}
func solution1(ransomNote string, magazine string) bool {
vMap := make(map[rune]int)
for _, val := range magazine {
vMap[val] += 1
}
for _, val := range ransomNote {
if vMap[val] > 0 {
vMap[val] -= 1
} else {
return false
}
}
return true
}
func solution2(ransomNote string, magazine string) bool {
cmpArr1 := make([]int, 26)
cmpArr2 := make([]int, 26)
for _, val := range ransomNote {
cmpArr1[val-'a'] += 1
}
for _, val := range magazine {
cmpArr2[val-'a'] += 1
}
for i := 0; i < 26; i++ {
if cmpArr1[i] > cmpArr2[2] {
return false
}
}
return true
}
// @lc code=end