Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9e1aad7

Browse files
authored
Create Count Common Words With One Occurrence.java
1 parent 665141b commit 9e1aad7

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int countWords(String[] words1, String[] words2) {
3+
Map<String, int[]> map = new HashMap<>();
4+
buildFrequencyMap(words1, map, 0);
5+
buildFrequencyMap(words2, map, 1);
6+
return (int) map.entrySet().stream()
7+
.filter(entry -> entry.getValue()[0] == 1 && entry.getValue()[1] == 1)
8+
.count();
9+
}
10+
11+
private void buildFrequencyMap(String[] words, Map<String, int[]> map, int idx) {
12+
for (String word : words) {
13+
map.computeIfAbsent(word, k -> new int[2])[idx]++;
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)