Here's the solution:
def count_unique_words(sentence):
words = sentence.split()
unique_words = set(words)
return len(unique_words)
Or more concisely:
def count_unique_words(sentence):
return len(set(sentence.split()))
Time: where is the number of words. Space: where is the number of unique words.
The set handles all the deduplication work. Your job is just to know it exists.