File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public List <String > removeAnagrams (String [] words ) {
3
+ List <String > result = new ArrayList <>();
4
+ int startIdx = 0 ;
5
+ for (int i = 1 ; i < words .length ; i ++) {
6
+ if (!isAnagram (words [i ], words [startIdx ])) {
7
+ result .add (words [startIdx ]);
8
+ startIdx = i ;
9
+ }
10
+ }
11
+ result .add (words [startIdx ]);
12
+ return result ;
13
+ }
14
+
15
+ private boolean isAnagram (String wordOne , String wordTwo ) {
16
+ if (wordOne .length () != wordTwo .length ()) {
17
+ return false ;
18
+ }
19
+ int [] counter = new int [26 ];
20
+ for (int i = 0 ; i < wordOne .length (); i ++) {
21
+ counter [wordOne .charAt (i ) - 'a' ]++;
22
+ counter [wordTwo .charAt (i ) - 'a' ]--;
23
+ }
24
+ for (int i = 0 ; i < 26 ; i ++) {
25
+ if (counter [i ] != 0 ) {
26
+ return false ;
27
+ }
28
+ }
29
+ return true ;
30
+ }
31
+ }
You can’t perform that action at this time.
0 commit comments