-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathFindResultantArrayAfterRemovingAnagrams.java
33 lines (30 loc) · 1.23 KB
/
FindResultantArrayAfterRemovingAnagrams.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// https://leetcode.com/problems/find-resultant-array-after-removing-anagrams
// T: O(|words| * |words[i]|)
// S: O(|words[i]|)
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FindResultantArrayAfterRemovingAnagrams {
public List<String> removeAnagrams(String[] words) {
final List<String> result = new ArrayList<>();
result.add(words[0]);
Map<Character, Integer> wordSignature = getCharacterFrequencies(words[0]);
for (int index = 1 ; index < words.length ; index++) {
final String word = words[index];
final Map<Character, Integer> wordCharFrequencies = getCharacterFrequencies(word);
if (!wordCharFrequencies.equals(wordSignature)) {
result.add(word);
wordSignature = wordCharFrequencies;
}
}
return result;
}
private Map<Character, Integer> getCharacterFrequencies(String string) {
final Map<Character, Integer> result = new HashMap<>();
for (int index = 0 ; index < string.length() ; index++) {
result.put(string.charAt(index), result.getOrDefault(string.charAt(index), 0) + 1);
}
return result;
}
}