I am trying to solve a problem of finding missing words in a string.
For example:
Input
String 1 = "I love programming, pizza, coke and chips";
String 2 = "I programming, chips";
Required Output
Missing words = "love pizza coke";
Note: the order of output has to be same (this is where I am messing up)
what I have so far, but I am getting wrong results.
public static List<String> findWord(String s, String t) {
HashSet<String> missingWords = new HashSet<>();
String[] words = s.split(" ");
for(int i=0; i < words.length; i++) {
if (!t.contains(words[i])) {
missingWords.add(words[i]);
}
}
List<String> ans = new ArrayList<String>();
for (String str: missingWords) {
ans.add(str);
}
return ans;
}