java: add «884. Uncommon Words from Two Sentences»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
11f539ddd6
commit
12f5d72bf2
1 changed files with 27 additions and 0 deletions
27
java/uncommon-words-from-two-sentences.java
Normal file
27
java/uncommon-words-from-two-sentences.java
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public String[] uncommonFromSentences(String s1, String s2) {
|
||||||
|
var counters = new HashMap<String, Integer>();
|
||||||
|
|
||||||
|
for (String word : s1.strip().split("\\s+")) {
|
||||||
|
counters.compute(word, (key, count) -> 1 + (count != null ? count : 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String word : s2.strip().split("\\s+")) {
|
||||||
|
counters.compute(word, (key, count) -> 1 + (count != null ? count : 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
var uncommon = new ArrayList<String>();
|
||||||
|
for (var entry : counters.entrySet()) {
|
||||||
|
if (entry.getValue() != 1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
uncommon.add(entry.getKey());
|
||||||
|
}
|
||||||
|
|
||||||
|
return uncommon.toArray(new String[0]);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue