mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
18
leetcode/0383.Ransom-Note/383.Ransom Note.go
Normal file
18
leetcode/0383.Ransom-Note/383.Ransom Note.go
Normal file
@ -0,0 +1,18 @@
|
||||
package leetcode
|
||||
|
||||
func canConstruct(ransomNote string, magazine string) bool {
|
||||
if len(ransomNote) > len(magazine) {
|
||||
return false
|
||||
}
|
||||
var cnt [26]int
|
||||
for _, v := range magazine {
|
||||
cnt[v-'a']++
|
||||
}
|
||||
for _, v := range ransomNote {
|
||||
cnt[v-'a']--
|
||||
if cnt[v-'a'] < 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
51
leetcode/0383.Ransom-Note/383.Ransom Note_test.go
Normal file
51
leetcode/0383.Ransom-Note/383.Ransom Note_test.go
Normal file
@ -0,0 +1,51 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question383 struct {
|
||||
para383
|
||||
ans383
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
type para383 struct {
|
||||
ransomNote string
|
||||
magazine string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
type ans383 struct {
|
||||
ans bool
|
||||
}
|
||||
|
||||
func Test_Problem383(t *testing.T) {
|
||||
|
||||
qs := []question383{
|
||||
|
||||
{
|
||||
para383{"a", "b"},
|
||||
ans383{false},
|
||||
},
|
||||
|
||||
{
|
||||
para383{"aa", "ab"},
|
||||
ans383{false},
|
||||
},
|
||||
|
||||
{
|
||||
para383{"aa", "aab"},
|
||||
ans383{true},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 383------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans383, q.para383
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, canConstruct(p.ransomNote, p.magazine))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
64
leetcode/0383.Ransom-Note/README.md
Normal file
64
leetcode/0383.Ransom-Note/README.md
Normal file
@ -0,0 +1,64 @@
|
||||
# [383. Ransom Note](https://leetcode-cn.com/problems/ransom-note/)
|
||||
|
||||
## 题目
|
||||
|
||||
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 <= 100000
|
||||
- ransomNote and magazine consist of lowercase English letters.
|
||||
|
||||
## 题目大意
|
||||
|
||||
为了不在赎金信中暴露字迹,从杂志上搜索各个需要的字母,组成单词来表达意思。
|
||||
|
||||
给你一个赎金信 (ransomNote) 字符串和一个杂志(magazine)字符串,判断 ransomNote 能不能由 magazines 里面的字符构成。
|
||||
|
||||
如果可以构成,返回 true ;否则返回 false 。
|
||||
|
||||
magazine 中的每个字符只能在 ransomNote 中使用一次。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- ransomNote和magazine都是由小写字母组成,所以用数组进行简单的字符统计
|
||||
|
||||
## 代码
|
||||
|
||||
````go
|
||||
package leetcode
|
||||
|
||||
func canConstruct(ransomNote string, magazine string) bool {
|
||||
if len(ransomNote) > len(magazine) {
|
||||
return false
|
||||
}
|
||||
var cnt [26]int
|
||||
for _, v := range magazine {
|
||||
cnt[v-'a']++
|
||||
}
|
||||
for _, v := range ransomNote {
|
||||
cnt[v-'a']--
|
||||
if cnt[v-'a'] < 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
````
|
Reference in New Issue
Block a user