A sentence is a string of single-space separated words where each word consists only of lowercase letters.
A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.
Given two sentences s1 and s2, return a list of all the uncommon words. You may return the answer in any order.
Example 1:
Input: s1 = "this apple is sweet", s2 = "this apple is sour"
Output: ["sweet","sour"]
Explanation:
The word "sweet" appears only in s1, while the word "sour" appears only in s2.
Example 2:
Input: s1 = "apple apple", s2 = "banana"
Output: ["banana"]
Constraints:
1 <= s1.length, s2.length <= 200s1 and s2 consist of lowercase English letters and spaces.s1 and s2 do not have leading or trailing spaces.s1 and s2 are separated by a single space.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method identifies uncommon words by meticulously checking each word in both sentences. We compare each word against every other word to see if it appears only once across the two sentences. This exhaustive comparison guarantees we find all uncommon words.
Here's how the algorithm would work step-by-step:
def uncommon_from_sentences(first_sentence, second_sentence):
first_sentence_words = first_sentence.split()
second_sentence_words = second_sentence.split()
uncommon_words = []
for word_from_first in first_sentence_words:
is_uncommon = True
# Checking against the first sentence
for another_word_first in first_sentence_words:
if (word_from_first == another_word_first and
first_sentence_words.index(word_from_first) !=
first_sentence_words.index(another_word_first)):
is_uncommon = False
break
# Checking against the second sentence
if is_uncommon:
for word_from_second in second_sentence_words:
if word_from_first == word_from_second:
is_uncommon = False
break
if is_uncommon:
uncommon_words.append(word_from_first)
for word_from_second in second_sentence_words:
is_uncommon = True
# Checking against the second sentence
for another_word_second in second_sentence_words:
if (word_from_second == another_word_second and
second_sentence_words.index(word_from_second) !=
second_sentence_words.index(another_word_second)):
is_uncommon = False
break
# Checking against the first sentence
if is_uncommon:
for word_from_first in first_sentence_words:
if word_from_second == word_from_first:
is_uncommon = False
break
if is_uncommon:
uncommon_words.append(word_from_second)
# Ensuring there are no duplicates
final_uncommon_words = []
for word in uncommon_words:
if word not in final_uncommon_words:
final_uncommon_words.append(word)
return final_uncommon_wordsThe best way to solve this is to count how often each word appears in both sentences. Then, we can easily identify words that appear exactly once in one of the sentences and not at all in the other sentence.
Here's how the algorithm would work step-by-step:
def find_uncommon_words(sentence1, sentence2):
words_sentence1 = sentence1.split()
words_sentence2 = sentence2.split()
frequency_sentence1 = {}
for word in words_sentence1:
frequency_sentence1[word] = frequency_sentence1.get(word, 0) + 1
frequency_sentence2 = {}
for word in words_sentence2:
frequency_sentence2[word] = frequency_sentence2.get(word, 0) + 1
uncommon_words = []
# Add words unique to sentence1
for word in words_sentence1:
if frequency_sentence1[word] == 1:
# Only consider words appearing once in sentence1
if word not in frequency_sentence2:
uncommon_words.append(word)
# Add words unique to sentence2
for word in words_sentence2:
if frequency_sentence2[word] == 1:
# Only consider words appearing once in sentence2
if word not in frequency_sentence1:
uncommon_words.append(word)
return uncommon_words| Case | How to Handle |
|---|---|
| One or both input sentences are empty strings | Return an empty list if either sentence is empty as no uncommon words can be found. |
| Input sentences contain leading/trailing whitespace | Trim whitespace from each sentence before processing to avoid incorrect word splitting. |
| Input sentences contain multiple spaces between words | Normalize spaces (e.g., by splitting and rejoining) to ensure correct word counting. |
| Input sentences contain punctuation marks | Remove or replace punctuation marks before processing words to avoid counting 'word!' differently from 'word'. |
| All words are common to both sentences | The result will be an empty list since no uncommon words exist. |
| One or both sentences contain very long words | The solution should handle very long words without causing memory issues or errors, perhaps by limiting word length. |
| Sentences with extremely large number of words (scalability) | Using a hash map for word counts allows for efficient O(n) time complexity even with large sentences. |
| Case sensitivity (same word with different capitalization) | Convert all words to lowercase before counting to treat words with different capitalization as the same. |