A sentence is a string of single-space separated phrases the place every phrase consists solely of lowercase letters.
A phrase is unusual if it seems precisely as soon as in one of many sentences, and doesn’t seem within the different sentence.
Given two sentences s1
and s2
, return a listing of all of the unusual phrases. You could return the reply in any order.
Instance 1:
Enter: s1 = “this apple is nice”, s2 = “this apple is bitter”
Output: [“sweet”,”sour”]
Rationalization:
The phrase "candy"
seems solely in s1
, whereas the phrase "bitter"
seems solely in s2
.
Instance 2:
Enter: s1 = “apple apple”, s2 = “banana”
Output: [“banana”]
class Answer {
public String[] uncommonFromSentences(String A, String B) {
Listing<String> ans = new ArrayList<>();
Map<String, Integer> rely = new HashMap<>();for (remaining String phrase : (A + ' ' + B).break up(" "))
rely.merge(phrase, 1, Integer::sum);
for (remaining String phrase : rely.keySet())
if (rely.get(phrase) == 1)
ans.add(phrase);
return ans.toArray(new String[0]);
}
}