Skip to content

[pull] main from doocs:main #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40,926 changes: 20,516 additions & 20,410 deletions images/starcharts.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
113 changes: 69 additions & 44 deletions solution/0300-0399/0383.Ransom Note/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@

<!-- 这里可写通用的实现逻辑 -->

用一个数组或字典 chars 存放 magazine 中每个字母出现的次数。遍历 ransomNote 中每个字母,判断 chars 是否包含即可。
**方法一:哈希表或数组**

我们可以用一个哈希表或长度为 $26$ 的数组 $cnt$ 记录字符串 `magazine` 中所有字符出现的次数。然后遍历字符串 `ransomNote`,对于其中的每个字符 $c$,我们将其从 $cnt$ 的次数减 $1$,如果减 $1$ 之后的次数小于 $0$,说明 $c$ 在 `magazine` 中出现的次数不够,因此无法构成 `ransomNote`,返回 $false$ 即可。

否则,遍历结束后,说明 `ransomNote` 中的每个字符都可以在 `magazine` 中找到对应的字符,因此返回 $true$。

时间复杂度 $O(m + n)$,空间复杂度 $O(C)$。其中 $m$ 和 $n$ 分别为字符串 `ransomNote` 和 `magazine` 的长度;而 $C$ 为字符集的大小,本题中 $C = 26$。

<!-- tabs:start -->

Expand All @@ -59,11 +65,11 @@
```python
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
counter = Counter(magazine)
cnt = Counter(magazine)
for c in ransomNote:
if counter[c] <= 0:
cnt[c] -= 1
if cnt[c] < 0:
return False
counter[c] -= 1
return True
```

Expand All @@ -74,50 +80,34 @@ class Solution:
```java
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] counter = new int[26];
for (char c : magazine.toCharArray()) {
++counter[c - 'a'];
int[] cnt = new int[26];
for (int i = 0; i < magazine.length(); ++i) {
++cnt[magazine.charAt(i) - 'a'];
}
for (char c : ransomNote.toCharArray()) {
if (counter[c - 'a'] <= 0) {
for (int i = 0; i < ransomNote.length(); ++i) {
if (--cnt[ransomNote.charAt(i) - 'a'] < 0) {
return false;
}
--counter[c - 'a'];
}
return true;
}
}
```

### **TypeScript**

```ts
function canConstruct(ransomNote: string, magazine: string): boolean {
let counter = new Array(26).fill(0);
let base = 'a'.charCodeAt(0);
for (let s of magazine) {
++counter[s.charCodeAt(0) - base];
}
for (let s of ransomNote) {
let idx = s.charCodeAt(0) - base;
if (counter[idx] == 0) return false;
--counter[idx];
}
return true;
}
```

### **C++**

```cpp
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
vector<int> counter(26);
for (char c : magazine) ++counter[c - 'a'];
for (char c : ransomNote) {
if (counter[c - 'a'] <= 0) return false;
--counter[c - 'a'];
int cnt[26]{};
for (char& c : magazine) {
++cnt[c - 'a'];
}
for (char& c : ransomNote) {
if (--cnt[c - 'a'] < 0) {
return false;
}
}
return true;
}
Expand All @@ -128,25 +118,60 @@ public:

```go
func canConstruct(ransomNote string, magazine string) bool {
rc := make([]int, 26)
for _, b := range ransomNote {
rc[b-'a']++
}

mc := make([]int, 26)
for _, b := range magazine {
mc[b-'a']++
cnt := [26]int{}
for _, c := range magazine {
cnt[c-'a']++
}

for i := 0; i < 26; i++ {
if rc[i] > mc[i] {
for _, c := range ransomNote {
cnt[c-'a']--
if cnt[c-'a'] < 0 {
return false
}
}
return true
}
```

### **TypeScript**

```ts
function canConstruct(ransomNote: string, magazine: string): boolean {
const cnt = new Array(26).fill(0);
for (const c of magazine) {
++cnt[c.charCodeAt(0) - 97];
}
for (const c of ransomNote) {
if (--cnt[c.charCodeAt(0) - 97] < 0) {
return false;
}
}
return true;
}
```

### **PHP**

```php
class Solution {
/**
* @param String $ransomNote
* @param String $magazine
* @return Boolean
*/
function canConstruct($ransomNote, $magazine) {
$arrM = str_split($magazine);
for ($i = 0; $i < strlen($magazine); $i++) {
$hashtable[$arrM[$i]] += 1;
}
for ($j = 0; $j < strlen($ransomNote); $j++) {
if (!isset($hashtable[$ransomNote[$j]]) || $hashtable[$ransomNote[$j]] == 0) return false;
else $hashtable[$ransomNote[$j]] -= 1;
}
return true;
}
}
```

### **...**

```
Expand Down
105 changes: 62 additions & 43 deletions solution/0300-0399/0383.Ransom Note/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
```python
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
counter = Counter(magazine)
cnt = Counter(magazine)
for c in ransomNote:
if counter[c] <= 0:
cnt[c] -= 1
if cnt[c] < 0:
return False
counter[c] -= 1
return True
```

Expand All @@ -49,50 +49,34 @@ class Solution:
```java
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] counter = new int[26];
for (char c : magazine.toCharArray()) {
++counter[c - 'a'];
int[] cnt = new int[26];
for (int i = 0; i < magazine.length(); ++i) {
++cnt[magazine.charAt(i) - 'a'];
}
for (char c : ransomNote.toCharArray()) {
if (counter[c - 'a'] <= 0) {
for (int i = 0; i < ransomNote.length(); ++i) {
if (--cnt[ransomNote.charAt(i) - 'a'] < 0) {
return false;
}
--counter[c - 'a'];
}
return true;
}
}
```

### **TypeScript**

```ts
function canConstruct(ransomNote: string, magazine: string): boolean {
let counter = new Array(26).fill(0);
let base = 'a'.charCodeAt(0);
for (let s of magazine) {
++counter[s.charCodeAt(0) - base];
}
for (let s of ransomNote) {
let idx = s.charCodeAt(0) - base;
if (counter[idx] == 0) return false;
--counter[idx];
}
return true;
}
```

### **C++**

```cpp
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
vector<int> counter(26);
for (char c : magazine) ++counter[c - 'a'];
for (char c : ransomNote) {
if (counter[c - 'a'] <= 0) return false;
--counter[c - 'a'];
int cnt[26]{};
for (char& c : magazine) {
++cnt[c - 'a'];
}
for (char& c : ransomNote) {
if (--cnt[c - 'a'] < 0) {
return false;
}
}
return true;
}
Expand All @@ -103,25 +87,60 @@ public:

```go
func canConstruct(ransomNote string, magazine string) bool {
rc := make([]int, 26)
for _, b := range ransomNote {
rc[b-'a']++
cnt := [26]int{}
for _, c := range magazine {
cnt[c-'a']++
}

mc := make([]int, 26)
for _, b := range magazine {
mc[b-'a']++
}

for i := 0; i < 26; i++ {
if rc[i] > mc[i] {
for _, c := range ransomNote {
cnt[c-'a']--
if cnt[c-'a'] < 0 {
return false
}
}
return true
}
```

### **TypeScript**

```ts
function canConstruct(ransomNote: string, magazine: string): boolean {
const cnt = new Array(26).fill(0);
for (const c of magazine) {
++cnt[c.charCodeAt(0) - 97];
}
for (const c of ransomNote) {
if (--cnt[c.charCodeAt(0) - 97] < 0) {
return false;
}
}
return true;
}
```

### **PHP**

```php
class Solution {
/**
* @param String $ransomNote
* @param String $magazine
* @return Boolean
*/
function canConstruct($ransomNote, $magazine) {
$arrM = str_split($magazine);
for ($i = 0; $i < strlen($magazine); $i++) {
$hashtable[$arrM[$i]] += 1;
}
for ($j = 0; $j < strlen($ransomNote); $j++) {
if (!isset($hashtable[$ransomNote[$j]]) || $hashtable[$ransomNote[$j]] == 0) return false;
else $hashtable[$ransomNote[$j]] -= 1;
}
return true;
}
}
```

### **...**

```
Expand Down
13 changes: 8 additions & 5 deletions solution/0300-0399/0383.Ransom Note/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
vector<int> counter(26);
for (char c : magazine) ++counter[c - 'a'];
for (char c : ransomNote) {
if (counter[c - 'a'] <= 0) return false;
--counter[c - 'a'];
int cnt[26]{};
for (char& c : magazine) {
++cnt[c - 'a'];
}
for (char& c : ransomNote) {
if (--cnt[c - 'a'] < 0) {
return false;
}
}
return true;
}
Expand Down
19 changes: 7 additions & 12 deletions solution/0300-0399/0383.Ransom Note/Solution.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
func canConstruct(ransomNote string, magazine string) bool {
rc := make([]int, 26)
for _, b := range ransomNote {
rc[b-'a']++
cnt := [26]int{}
for _, c := range magazine {
cnt[c-'a']++
}

mc := make([]int, 26)
for _, b := range magazine {
mc[b-'a']++
}

for i := 0; i < 26; i++ {
if rc[i] > mc[i] {
for _, c := range ransomNote {
cnt[c-'a']--
if cnt[c-'a'] < 0 {
return false
}
}
return true
}
}
11 changes: 5 additions & 6 deletions solution/0300-0399/0383.Ransom Note/Solution.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] counter = new int[26];
for (char c : magazine.toCharArray()) {
++counter[c - 'a'];
int[] cnt = new int[26];
for (int i = 0; i < magazine.length(); ++i) {
++cnt[magazine.charAt(i) - 'a'];
}
for (char c : ransomNote.toCharArray()) {
if (counter[c - 'a'] <= 0) {
for (int i = 0; i < ransomNote.length(); ++i) {
if (--cnt[ransomNote.charAt(i) - 'a'] < 0) {
return false;
}
--counter[c - 'a'];
}
return true;
}
Expand Down
Loading