Skip to content

Commit 481b1dd

Browse files
committed
Merge pull request halfrost#207 from gostool/leetcode0383
Leetcode0383
2 parents 75074a7 + 6cb7b1a commit 481b1dd

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode
2+
3+
func canConstruct(ransomNote string, magazine string) bool {
4+
if len(ransomNote) > len(magazine) {
5+
return false
6+
}
7+
var cnt [26]int
8+
for _, v := range magazine {
9+
cnt[v-'a']++
10+
}
11+
for _, v := range ransomNote {
12+
cnt[v-'a']--
13+
if cnt[v-'a'] < 0 {
14+
return false
15+
}
16+
}
17+
return true
18+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question383 struct {
9+
para383
10+
ans383
11+
}
12+
13+
// para 是参数
14+
type para383 struct {
15+
ransomNote string
16+
magazine string
17+
}
18+
19+
// ans 是答案
20+
type ans383 struct {
21+
ans bool
22+
}
23+
24+
func Test_Problem383(t *testing.T) {
25+
26+
qs := []question383{
27+
28+
{
29+
para383{"a", "b"},
30+
ans383{false},
31+
},
32+
33+
{
34+
para383{"aa", "ab"},
35+
ans383{false},
36+
},
37+
38+
{
39+
para383{"aa", "aab"},
40+
ans383{true},
41+
},
42+
}
43+
44+
fmt.Printf("------------------------Leetcode Problem 383------------------------\n")
45+
46+
for _, q := range qs {
47+
_, p := q.ans383, q.para383
48+
fmt.Printf("【input】:%v 【output】:%v\n", p, canConstruct(p.ransomNote, p.magazine))
49+
}
50+
fmt.Printf("\n\n\n")
51+
}

leetcode/0383.Ransom-Note/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# [383. Ransom Note](https://leetcode-cn.com/problems/ransom-note/)
2+
3+
## 题目
4+
5+
Given two stings ransomNote and magazine, return true if ransomNote can be constructed from magazine and false otherwise.
6+
7+
Each letter in magazine can only be used once in ransomNote.
8+
9+
**Example 1**:
10+
11+
Input: ransomNote = "a", magazine = "b"
12+
Output: false
13+
14+
**Example 2**:
15+
16+
Input: ransomNote = "aa", magazine = "ab"
17+
Output: false
18+
19+
**Example 3**:
20+
21+
Input: ransomNote = "aa", magazine = "aab"
22+
Output: true
23+
24+
**Constraints:**
25+
26+
- 1 <= ransomNote.length, magazine.length <= 100000
27+
- ransomNote and magazine consist of lowercase English letters.
28+
29+
## 题目大意
30+
31+
为了不在赎金信中暴露字迹,从杂志上搜索各个需要的字母,组成单词来表达意思。
32+
33+
给你一个赎金信 (ransomNote) 字符串和一个杂志(magazine)字符串,判断 ransomNote 能不能由 magazines 里面的字符构成。
34+
35+
如果可以构成,返回 true ;否则返回 false 。
36+
37+
magazine 中的每个字符只能在 ransomNote 中使用一次。
38+
39+
## 解题思路
40+
41+
- ransomNote和magazine都是由小写字母组成,所以用数组进行简单的字符统计
42+
43+
## 代码
44+
45+
````go
46+
package leetcode
47+
48+
func canConstruct(ransomNote string, magazine string) bool {
49+
if len(ransomNote) > len(magazine) {
50+
return false
51+
}
52+
var cnt [26]int
53+
for _, v := range magazine {
54+
cnt[v-'a']++
55+
}
56+
for _, v := range ransomNote {
57+
cnt[v-'a']--
58+
if cnt[v-'a'] < 0 {
59+
return false
60+
}
61+
}
62+
return true
63+
}
64+
````

0 commit comments

Comments
 (0)