Find the Missing words from 2 sentences

Viewed 4350

There are two strings, s and t, where t is a sub-sequence of s, report the words of s missing in t(case sensitive) in the order they are missing.

Constraints : Strings s and t consist of English letters, dash and spaces only. All words are delimited by space.

Example : If s =" I am using computer to improve my work" and t="am computer to improve", then the output of missing words should be : "I using my work"

1 Answers
s = " I am using computer to improve my work"  
t = "am computer to improve"
s_list=s.split()
t_list=t.split()

# Using set() 
def Diff1(li1, li2): 
    return (list(set(li1) - set(li2)))

# Not using set() 
def Diff2(li1, li2): 
    li_dif = [i for i in li1 + li2 if i not in li1 or i not in li2] 
    return li_dif

if __name__ == "__main__":
    print(Diff1(s_list,t_list))
    print(Diff2(s_list,t_list))

Related