Skip to content

Instantly share code, notes, and snippets.

@mmloveaa
Last active November 1, 2023 09:52
Show Gist options
  • Save mmloveaa/af6ac0a1fd28249abf63 to your computer and use it in GitHub Desktop.
Save mmloveaa/af6ac0a1fd28249abf63 to your computer and use it in GitHub Desktop.
Missing Word
Julia and Samantha are playing with strings. Julia has a string S, and Samantha has a string T which is a subsequence of string S. They are trying to find out what words are missing in T.
Help Julia and Samantha to solve the problem. List all the missing words in T, such that inserting them at the appropriate positions in T, in the same order, results in the string S.
Constraints
1 <= |T| <= |S| <= 106, where |X| denotes the length of string X.
The length of each word will be less than 15.
Function Parameter
You are given a function missingWords that takes the strings S and T as its arguments.
Function Return Value
Return an array of the missing words.
Sample Input
I am using hackerrank to improve programming
am hackerrank to improve
Sample Output
I
using
programming
Explanation
Missing words are:
1. I
2. using
3. programming
function missingWords(s, t) {
var missing = [];
var a = s. split(' ');
var b = t.split(' ');
for(var i=0, j=0; i < a.length; i++){
if(a[i] !== b[j]) {
missing.push(a[i]);
} else {
j++;
}
}
return missing;
}
missingWords('I am using hackerrank to improve programming', 'am hackerrank to improve')
// method 2
// function missingWords(s, t) {
// var missing = [];
// var a = s. split(' ');
// var b = t.split(' ');
// a.forEach((word, i) => {
// if(word !== b[i- missing.length]) {
// missing.push(word);
// }
// });
// return missing;
// }
// method 3
// function missingWords(x, y) {
// var a=x.split(' '), s=y.split(' '), m=[];
// for(var i=0;i<a.length;i++)
// if(a[i]!==s[i-m.length])
// m.push(a[i]);
// return m;
// }
// missingWords('I am using hackerrank to improve programming', 'am hackerrank to improve')
@shariqislam786
Copy link

actually this problem itself is ambigous. consider
S: i am programmer and programmer and coder
T: i am programmer and coder

answer 1: and programmer (if you insert before and)
answer 2: programmer and (if you insert after and)

@MaxPower007-Rs
Copy link

MaxPower007-Rs commented Oct 14, 2019

test this code:

def missingWords(s, t):
    missing = [];
    a = s.split(' ');
    b = t.split(' ');    
    missing = list(set(a).symmetric_difference(b))      
    
    return missing

@Urvashi-91
Copy link

def missing words(s, t):
a = s.split()
b = t.split()
m = []
if len(a) == len(b):
print ("No missing word")
else:
for i in range(len(a)):
if a[i] in b:
continue
else:
m.append(a[i])
return m

@jarvissilva937
Copy link

Thank you for your solutions, I found this great website which has many hackerrank solutions in python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment