Uncommon Words from Two Sentences

Easy Topics Company Tags

A word is uncommon if it occurs exactly once in one sentence and never in the other. Given the sentences s1 and s2, return all uncommon words in any order. Each sentence contains lowercase English words separated by single spaces, with no leading or trailing spaces.

Example 1:

Input: s1 = "red blue"
s2 = "red green"

Output: ["blue","green"]

Explanation: The word "red" occurs in both sentences, so it is excluded. "blue" and "green" each occur once overall and are uncommon.

Example 2:

Input: s1 = "cat cat"
s2 = "dog"

Output: ["dog"]

Explanation: "cat" appears twice in the first sentence and is excluded. "dog" appears once in the second sentence and never in the first, so it is the only uncommon word.

Example 3:

Input: s1 = "a b"
s2 = "b a"

Output: []

Explanation: Both "a" and "b" appear in both sentences. Neither word is uncommon, so return an empty array.

Constraints:

  • 1 <= s1.length, s2.length <= 200
  • Both sentences contain only lowercase English letters and spaces.
  • Words are separated by one space, with no leading or trailing spaces.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.