1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00

java: add «884. Uncommon Words from Two Sentences»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-09-17 18:13:29 +02:00
parent 11f539ddd6
commit 12f5d72bf2
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View 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]);
}
}