Guest User

Untitled

a guest
Oct 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. 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.
  2. 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.
  3. Constraints
  4. 1 <= |T| <= |S| <= 106, where |X| denotes the length of string X.
  5. The length of each word will be less than 15.
  6.  
  7. Function Parameter
  8. You are given a function missingWords that takes the strings S and T as its arguments.
  9.  
  10. Function Return Value
  11. Return an array of the missing words.
  12.  
  13. Sample Input
  14. I am using hackerrank to improve programming
  15. am hackerrank to improve
  16. Sample Output
  17. I
  18. using
  19. programming
  20. Explanation
  21. Missing words are:
  22. 1. I
  23. 2. using
  24. 3. programming
  25.  
  26. function missingWords(s, t) {
  27.  
  28. var missing = [];
  29. var a = s. split(' ');
  30. var b = t.split(' ');
  31.  
  32. for(var i=0, j=0; i < a.length; i++){
  33. if(a[i] !== b[j]) {
  34. missing.push(a[i]);
  35. } else {
  36. j++;
  37. }
  38. }
  39.  
  40. return missing;
  41. }
  42.  
  43. missingWords('I am using hackerrank to improve programming', 'am hackerrank to improve')
  44.  
  45.  
  46. // method 2
  47. // function missingWords(s, t) {
  48.  
  49. // var missing = [];
  50. // var a = s. split(' ');
  51. // var b = t.split(' ');
  52.  
  53. // a.forEach((word, i) => {
  54. // if(word !== b[i- missing.length]) {
  55. // missing.push(word);
  56. // }
  57. // });
  58.  
  59. // return missing;
  60. // }
  61.  
  62. // method 3
  63. // function missingWords(x, y) {
  64. // var a=x.split(' '), s=y.split(' '), m=[];
  65. // for(var i=0;i<a.length;i++)
  66. // if(a[i]!==s[i-m.length])
  67. // m.push(a[i]);
  68. // return m;
  69. // }
  70.  
  71. // missingWords('I am using hackerrank to improve programming', 'am hackerrank to improve')
Add Comment
Please, Sign In to add comment