Skip to content

Commit fef66df

Browse files
committed
feat: add typescript solution to lc problem: No.0383.Ransom Note
1 parent a3cf875 commit fef66df

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ class Solution {
8989
}
9090
```
9191

92+
### **TypeScript**
93+
94+
```ts
95+
function canConstruct(ransomNote: string, magazine: string): boolean {
96+
let counter = new Array(26).fill(0);
97+
let base = 'a'.charCodeAt(0);
98+
for (let s of magazine) {
99+
++counter[s.charCodeAt(0) - base];
100+
}
101+
for (let s of ransomNote) {
102+
let idx = s.charCodeAt(0) - base;
103+
if (counter[idx] == 0) return false;
104+
--counter[idx];
105+
}
106+
return true;
107+
};
108+
```
109+
92110
### **Go**
93111

94112
```go

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@ class Solution {
6666
}
6767
```
6868

69+
### **TypeScript**
70+
71+
```ts
72+
function canConstruct(ransomNote: string, magazine: string): boolean {
73+
let counter = new Array(26).fill(0);
74+
let base = 'a'.charCodeAt(0);
75+
for (let s of magazine) {
76+
++counter[s.charCodeAt(0) - base];
77+
}
78+
for (let s of ransomNote) {
79+
let idx = s.charCodeAt(0) - base;
80+
if (counter[idx] == 0) return false;
81+
--counter[idx];
82+
}
83+
return true;
84+
};
85+
```
86+
6987
### **Go**
7088

7189
```go
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function canConstruct(ransomNote: string, magazine: string): boolean {
2+
let counter = new Array(26).fill(0);
3+
let base = 'a'.charCodeAt(0);
4+
for (let s of magazine) {
5+
++counter[s.charCodeAt(0) - base];
6+
}
7+
for (let s of ransomNote) {
8+
let idx = s.charCodeAt(0) - base;
9+
if (counter[idx] == 0) return false;
10+
--counter[idx];
11+
}
12+
return true;
13+
};

0 commit comments

Comments
 (0)