Problem statement

https://leetcode.com/problems/uncommon-words-from-two-sentences/

Solution

What is actually asked is to find all words, such that their frequency is equal to 1. Let us concatenate two strings, and then split it and apply Counter function.

Complexity

Time complexity is O(M + N), where M and N are lengths of strings A and B and space complexity is O(M + N) as well.

Code

class Solution:
    def uncommonFromSentences(self, A, B):
        return[w for w, f in Counter((A+" "+B).split()).items() if f==1]