Leetcode in Java && Oracle
421 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
image_2022-01-05_01-37-27.png
56.5 KB
#N884. Uncommon Words from Two Sentences
problem link

#solution
class Solution {
public String[] uncommonFromSentences(String s1, String s2) {
Map<String, Integer> map = new HashMap<>();
for(String word: s1.split(" ")){
map.put(word, map.getOrDefault(word, 0) +1);
}

for(String word: s2.split(" ")){
map.put(word, map.getOrDefault(word, 0) +1);
}

List<String> list = new ArrayList<>();
for(Map.Entry<String, Integer> entry: map.entrySet()){
if(entry.getValue() == 1)
list.add(entry.getKey());
}

return list.toArray(new String[0]);
}
}