File tree 3 files changed +70
-0
lines changed
solution/0400-0499/0438.Find All Anagrams in a String
3 files changed +70
-0
lines changed Original file line number Diff line number Diff line change @@ -139,6 +139,31 @@ class Solution {
139
139
}
140
140
```
141
141
142
+ ### ** TypeScript**
143
+
144
+ ``` ts
145
+ function findAnagrams(s : string , p : string ): number [] {
146
+ let n = s .length , m = p .length ;
147
+ let cnt = new Array (26 ).fill (0 );
148
+ let ans = [];
149
+ for (let i = 0 ; i < m ; i ++ ) {
150
+ cnt [p .charCodeAt (i ) - 97 ]-- ;
151
+ cnt [s .charCodeAt (i ) - 97 ]++ ;
152
+ }
153
+ if (cnt .every (v => v == 0 )) {
154
+ ans .push (0 );
155
+ }
156
+ for (let i = m ; i < n ; i ++ ) {
157
+ cnt [s .charCodeAt (i ) - 97 ]++ ;
158
+ cnt [s .charCodeAt (i - m ) - 97 ]-- ;
159
+ if (cnt .every (v => v == 0 )) {
160
+ ans .push (i - m + 1 );
161
+ }
162
+ }
163
+ return ans ;
164
+ };
165
+ ```
166
+
142
167
### ** C++**
143
168
144
169
``` cpp
Original file line number Diff line number Diff line change @@ -89,6 +89,31 @@ class Solution {
89
89
}
90
90
```
91
91
92
+ ### ** TypeScript**
93
+
94
+ ``` ts
95
+ function findAnagrams(s : string , p : string ): number [] {
96
+ let n = s .length , m = p .length ;
97
+ let cnt = new Array (26 ).fill (0 );
98
+ let ans = [];
99
+ for (let i = 0 ; i < m ; i ++ ) {
100
+ cnt [p .charCodeAt (i ) - 97 ]-- ;
101
+ cnt [s .charCodeAt (i ) - 97 ]++ ;
102
+ }
103
+ if (cnt .every (v => v == 0 )) {
104
+ ans .push (0 );
105
+ }
106
+ for (let i = m ; i < n ; i ++ ) {
107
+ cnt [s .charCodeAt (i ) - 97 ]++ ;
108
+ cnt [s .charCodeAt (i - m ) - 97 ]-- ;
109
+ if (cnt .every (v => v == 0 )) {
110
+ ans .push (i - m + 1 );
111
+ }
112
+ }
113
+ return ans ;
114
+ };
115
+ ```
116
+
92
117
### ** C++**
93
118
94
119
``` cpp
Original file line number Diff line number Diff line change
1
+ function findAnagrams ( s : string , p : string ) : number [ ] {
2
+ let n = s . length , m = p . length ;
3
+ let cnt = new Array ( 26 ) . fill ( 0 ) ;
4
+ let ans = [ ] ;
5
+ for ( let i = 0 ; i < m ; i ++ ) {
6
+ cnt [ p . charCodeAt ( i ) - 97 ] -- ;
7
+ cnt [ s . charCodeAt ( i ) - 97 ] ++ ;
8
+ }
9
+ if ( cnt . every ( v => v == 0 ) ) {
10
+ ans . push ( 0 ) ;
11
+ }
12
+ for ( let i = m ; i < n ; i ++ ) {
13
+ cnt [ s . charCodeAt ( i ) - 97 ] ++ ;
14
+ cnt [ s . charCodeAt ( i - m ) - 97 ] -- ;
15
+ if ( cnt . every ( v => v == 0 ) ) {
16
+ ans . push ( i - m + 1 ) ;
17
+ }
18
+ }
19
+ return ans ;
20
+ } ;
You can’t perform that action at this time.
0 commit comments