Uncommon Words from Two Sentences
Given two sentences, find the words that occur only once.
See the explanation on leetcode.
This problem is almost trivial - there are just some strange things about non-deterministic runtimes and test cases. For instance, I found that the following solution scored very well on some runs but terribly in other runs
class Solution:
def uncommonFromSentences(self, s1: str, s2: str) -> list[str]:
if s1 == s2:
return list()
all = s1.split(" ") + s2.split(" ")
= dict()
counts
for word in all:
= counts.get(word, 0) + 1
counts[word]
return list(word for word, count in counts.items() if count == 1)
Further the gaurding statement results in an incredible increase in performance, which says more about the test cases than anything.
import pytest
class Solution:
def uncommonFromSentences(self, s1: str, s2: str) -> list[str]:
all = s1.split(" ") + s2.split(" ")
dict[str, int] = dict()
counts:
for word in all:
= counts.get(word, 0) + 1
counts[word]
return list(word for word, count in counts.items() if count == 1)
@pytest.fixture
def solution():
return Solution()
@pytest.mark.parametrize(
"s1, s2, answer",
("this apple is sweet", "this apple is sour", ["sweet", "sour"]),
("apple apple", "banana", ["banana"]),
(
("what is wrong with you",
"what is wrong with the world",
"you", "the", "world"],
[
),
),
)def test_solution(solution: Solution, s1: str, s2: str, answer: str):
= solution.uncommonFromSentences(s1, s2)
got print(got, answer)
assert sorted(got) == sorted(answer)
After seeing how poorly this performed, I looked at a number of submissions and the submissions did not appear to apply much different algorithms. I then decided to rerun another submission to find that it did not perform as well as on the submission.