Skip to content

Commit 316d63c

Browse files
committedApr 21, 2023
feat: add solutions to lc problem: No.0383
No.0383.Ransom Note
1 parent 8731833 commit 316d63c

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed
 

‎solution/0000-0099/0076.Minimum Window Substring/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Since the largest window of s only has one 'a', return empty string.
4949

5050
## Solutions
5151

52-
**Approach 1: Counting + Double Pointers**
52+
**Approach 1: Counting + Two Pointers**
5353

5454
We use a hash table or array $need$ to count the number of characters in string $t$, and use another hash table or array $window$ to count the number of characters in the sliding window. In addition, define two pointers $j$ and $i$ pointing to the left and right boundaries of the window, and the variable $cnt$ represents the number of characters in $t$ that are already included in the window. The variables $k$ and $mi$ represent the starting position and length of the minimum cover substring respectively.
5555

‎solution/0300-0399/0383.Ransom Note/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,25 @@ function canConstruct(ransomNote: string, magazine: string): boolean {
149149
}
150150
```
151151

152+
### **C#**
153+
154+
```cs
155+
public class Solution {
156+
public bool CanConstruct(string ransomNote, string magazine) {
157+
int[] cnt = new int[26];
158+
foreach (var c in magazine) {
159+
++cnt[c - 'a'];
160+
}
161+
foreach (var c in ransomNote) {
162+
if (--cnt[c - 'a'] < 0) {
163+
return false;
164+
}
165+
}
166+
return true;
167+
}
168+
}
169+
```
170+
152171
### **PHP**
153172

154173
```php

‎solution/0300-0399/0383.Ransom Note/README_EN.md

+27
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929

3030
## Solutions
3131

32+
**Approach 1: Hash Table or Array**
33+
34+
We can use a hash table or an array $cnt$ of length $26$ to record the number of times each character appears in the string `magazine`. Then traverse the string `ransomNote`, for each character $c$ in it, we decrease the number of $c$ by $1$ in $cnt$. If the number of $c$ is less than $0$ after the decrease, it means that the number of $c$ in `magazine` is not enough, so it cannot be composed of `ransomNote`, just return $false$.
35+
36+
Otherwise, after the traversal, it means that each character in `ransomNote` can be found in `magazine`. Therefore, return $true$.
37+
38+
The time complexity is $O(m + n)$, and the space complexity is $O(C)$. Where $m$ and $n$ are the lengths of the strings `ransomNote` and `magazine` respectively; and $C$ is the size of the character set, which is $26$ in this question.
39+
3240
<!-- tabs:start -->
3341

3442
### **Python3**
@@ -118,6 +126,25 @@ function canConstruct(ransomNote: string, magazine: string): boolean {
118126
}
119127
```
120128

129+
### **C#**
130+
131+
```cs
132+
public class Solution {
133+
public bool CanConstruct(string ransomNote, string magazine) {
134+
int[] cnt = new int[26];
135+
foreach (var c in magazine) {
136+
++cnt[c - 'a'];
137+
}
138+
foreach (var c in ransomNote) {
139+
if (--cnt[c - 'a'] < 0) {
140+
return false;
141+
}
142+
}
143+
return true;
144+
}
145+
}
146+
```
147+
121148
### **PHP**
122149

123150
```php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Solution {
2+
public bool CanConstruct(string ransomNote, string magazine) {
3+
int[] cnt = new int[26];
4+
foreach (var c in magazine) {
5+
++cnt[c - 'a'];
6+
}
7+
foreach (var c in ransomNote) {
8+
if (--cnt[c - 'a'] < 0) {
9+
return false;
10+
}
11+
}
12+
return true;
13+
}
14+
}

0 commit comments

Comments
 (0)
Please sign in to comment.