File tree Expand file tree Collapse file tree 4 files changed +191
-0
lines changed
solution/2200-2299/2273.Find Resultant Array After Removing Anagrams Expand file tree Collapse file tree 4 files changed +191
-0
lines changed Original file line number Diff line number Diff line change @@ -230,6 +230,73 @@ function removeAnagrams(words: string[]): string[] {
230230}
231231```
232232
233+ #### Rust
234+
235+ ``` rust
236+ impl Solution {
237+ pub fn remove_anagrams (words : Vec <String >) -> Vec <String > {
238+ fn check (s : & str , t : & str ) -> bool {
239+ if s . len () != t . len () {
240+ return true ;
241+ }
242+ let mut cnt = [0 ; 26 ];
243+ for c in s . bytes () {
244+ cnt [(c - b 'a' ) as usize ] += 1 ;
245+ }
246+ for c in t . bytes () {
247+ let idx = (c - b 'a' ) as usize ;
248+ cnt [idx ] -= 1 ;
249+ if cnt [idx ] < 0 {
250+ return true ;
251+ }
252+ }
253+ false
254+ }
255+
256+ let mut ans = vec! [words [0 ]. clone ()];
257+ for i in 1 .. words . len () {
258+ if check (& words [i - 1 ], & words [i ]) {
259+ ans . push (words [i ]. clone ());
260+ }
261+ }
262+ ans
263+ }
264+ }
265+ ```
266+
267+ #### JavaScript
268+
269+ ``` js
270+ /**
271+ * @param {string[]} words
272+ * @return {string[]}
273+ */
274+ var removeAnagrams = function (words ) {
275+ const ans = [words[0 ]];
276+ const check = (s , t ) => {
277+ if (s .length !== t .length ) {
278+ return true ;
279+ }
280+ const cnt = Array (26 ).fill (0 );
281+ for (const c of s) {
282+ ++ cnt[c .charCodeAt () - 97 ];
283+ }
284+ for (const c of t) {
285+ if (-- cnt[c .charCodeAt () - 97 ] < 0 ) {
286+ return true ;
287+ }
288+ }
289+ return false ;
290+ };
291+ for (let i = 1 ; i < words .length ; ++ i) {
292+ if (check (words[i - 1 ], words[i])) {
293+ ans .push (words[i]);
294+ }
295+ }
296+ return ans;
297+ };
298+ ```
299+
233300<!-- tabs:end -->
234301
235302<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -223,6 +223,73 @@ function removeAnagrams(words: string[]): string[] {
223223}
224224```
225225
226+ #### Rust
227+
228+ ``` rust
229+ impl Solution {
230+ pub fn remove_anagrams (words : Vec <String >) -> Vec <String > {
231+ fn check (s : & str , t : & str ) -> bool {
232+ if s . len () != t . len () {
233+ return true ;
234+ }
235+ let mut cnt = [0 ; 26 ];
236+ for c in s . bytes () {
237+ cnt [(c - b 'a' ) as usize ] += 1 ;
238+ }
239+ for c in t . bytes () {
240+ let idx = (c - b 'a' ) as usize ;
241+ cnt [idx ] -= 1 ;
242+ if cnt [idx ] < 0 {
243+ return true ;
244+ }
245+ }
246+ false
247+ }
248+
249+ let mut ans = vec! [words [0 ]. clone ()];
250+ for i in 1 .. words . len () {
251+ if check (& words [i - 1 ], & words [i ]) {
252+ ans . push (words [i ]. clone ());
253+ }
254+ }
255+ ans
256+ }
257+ }
258+ ```
259+
260+ #### JavaScript
261+
262+ ``` js
263+ /**
264+ * @param {string[]} words
265+ * @return {string[]}
266+ */
267+ var removeAnagrams = function (words ) {
268+ const ans = [words[0 ]];
269+ const check = (s , t ) => {
270+ if (s .length !== t .length ) {
271+ return true ;
272+ }
273+ const cnt = Array (26 ).fill (0 );
274+ for (const c of s) {
275+ ++ cnt[c .charCodeAt () - 97 ];
276+ }
277+ for (const c of t) {
278+ if (-- cnt[c .charCodeAt () - 97 ] < 0 ) {
279+ return true ;
280+ }
281+ }
282+ return false ;
283+ };
284+ for (let i = 1 ; i < words .length ; ++ i) {
285+ if (check (words[i - 1 ], words[i])) {
286+ ans .push (words[i]);
287+ }
288+ }
289+ return ans;
290+ };
291+ ```
292+
226293<!-- tabs:end -->
227294
228295<!-- solution:end -->
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string[] } words
3+ * @return {string[] }
4+ */
5+ var removeAnagrams = function ( words ) {
6+ const ans = [ words [ 0 ] ] ;
7+ const check = ( s , t ) => {
8+ if ( s . length !== t . length ) {
9+ return true ;
10+ }
11+ const cnt = Array ( 26 ) . fill ( 0 ) ;
12+ for ( const c of s ) {
13+ ++ cnt [ c . charCodeAt ( ) - 97 ] ;
14+ }
15+ for ( const c of t ) {
16+ if ( -- cnt [ c . charCodeAt ( ) - 97 ] < 0 ) {
17+ return true ;
18+ }
19+ }
20+ return false ;
21+ } ;
22+ for ( let i = 1 ; i < words . length ; ++ i ) {
23+ if ( check ( words [ i - 1 ] , words [ i ] ) ) {
24+ ans . push ( words [ i ] ) ;
25+ }
26+ }
27+ return ans ;
28+ } ;
Original file line number Diff line number Diff line change 1+ impl Solution {
2+ pub fn remove_anagrams ( words : Vec < String > ) -> Vec < String > {
3+ fn check ( s : & str , t : & str ) -> bool {
4+ if s. len ( ) != t. len ( ) {
5+ return true ;
6+ }
7+ let mut cnt = [ 0 ; 26 ] ;
8+ for c in s. bytes ( ) {
9+ cnt[ ( c - b'a' ) as usize ] += 1 ;
10+ }
11+ for c in t. bytes ( ) {
12+ let idx = ( c - b'a' ) as usize ;
13+ cnt[ idx] -= 1 ;
14+ if cnt[ idx] < 0 {
15+ return true ;
16+ }
17+ }
18+ false
19+ }
20+
21+ let mut ans = vec ! [ words[ 0 ] . clone( ) ] ;
22+ for i in 1 ..words. len ( ) {
23+ if check ( & words[ i - 1 ] , & words[ i] ) {
24+ ans. push ( words[ i] . clone ( ) ) ;
25+ }
26+ }
27+ ans
28+ }
29+ }
You can’t perform that action at this time.
0 commit comments