-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path383-RansomNote.go
More file actions
52 lines (43 loc) · 1.58 KB
/
383-RansomNote.go
File metadata and controls
52 lines (43 loc) · 1.58 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
package main
// 383. Ransom Note
// Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters 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.
import "fmt"
func canConstruct(ransomNote string, magazine string) bool {
if len(ransomNote) > len(magazine) {
return false
}
cnt := make([]int, 26) // ransomNote and magazine consist of lowercase English letters.
for _, v := range magazine { // 统计出杂志中的字母数
cnt[v-'a']++
}
for _, v := range ransomNote {
cnt[v-'a']-- // 依次减去赎金信需要用掉的字母
if cnt[v-'a'] < 0 { // 如果字符不够返回 false
return false
}
}
return true
}
func main() {
fmt.Println(canConstruct("a", "b")) // false
fmt.Println(canConstruct("aa", "ab")) // false
fmt.Println(canConstruct("aa", "aab")) // true
fmt.Println(canConstruct("ab", "bac")) // true
fmt.Println(canConstruct("cab", "ba")) // false
fmt.Println(canConstruct("abc", "abcc")) // true
fmt.Println(canConstruct("", "abcc")) // true
fmt.Println(canConstruct("abc", "")) // false
}