File tree Expand file tree Collapse file tree 3 files changed +49
-0
lines changed
solution/0300-0399/0383.Ransom Note Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -89,6 +89,24 @@ class Solution {
89
89
}
90
90
```
91
91
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
+
92
110
### ** Go**
93
111
94
112
``` go
Original file line number Diff line number Diff line change @@ -66,6 +66,24 @@ class Solution {
66
66
}
67
67
```
68
68
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
+
69
87
### ** Go**
70
88
71
89
``` go
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments