Skip to content

Commit 9f76b51

Browse files
authored
Create Find Resultant Array After Removing Anagrams.java
1 parent a41e4bb commit 9f76b51

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

0 commit comments

Comments
 (0)