Guest User

Untitled

a guest
Sep 21st, 2023
1,497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | Source Code | 0 0
  1. # https://leetcode.com/problems/uncommon-words-from-two-sentences/
  2.  
  3. class Solution:
  4.     def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:
  5.         s1_list = s1.split()
  6.         s2_list = s2.split()
  7.         words_dict = {}
  8.         uncommon_list = []
  9.         only_in_s1 = []
  10.         only_in_s2  = []
  11.         for x1 in s1_list:
  12.             if x1 not in s2_list:
  13.                 only_in_s1.append(x1)
  14.             else:
  15.                 pass
  16.         for x2 in s2_list:
  17.             if x2 not in s1_list:
  18.                 only_in_s2.append(x2)
  19.             else:
  20.                 pass
  21.         for word in only_in_s1:
  22.             if word in words_dict:
  23.                 words_dict[word] +=1
  24.             else:
  25.                 words_dict[word] = 1
  26.         for word in only_in_s2:
  27.             if word in words_dict:
  28.                 words_dict[word]+=1
  29.             else:
  30.                 words_dict[word] = 1
  31.         for uncommon in words_dict:
  32.             if words_dict.get(uncommon) == 1:
  33.                 uncommon_list.append(uncommon)
  34.             else:
  35.                 pass
  36.         return uncommon_list
  37.        
  38.        
  39.  
  40.  
  41.          
  42.  
  43.  
  44.        
Advertisement
Add Comment
Please, Sign In to add comment