Problem
Given two strings, you have to find the missing string.
Example
Given a string str1 = This is an example
Given another string str2 = is example
Return ["This", "an"]
Solution
public class Solution {
/*
* @param : a given string
* @param : another given string
* @return: An array of missing string
*/
public List<String> missingString(String str1, String str2) {
// Write your code here
List<String> res = new ArrayList<>();
String[] s1 = str1.split(" ");
String[] s2 = str2.split(" ");
if (s1.length == s2.length) {
return res;
}
//assume s1.length > s2.length
if (s1.length < s2.length) {
String[] temp = s1;
s1 = s2;
s2 = temp;
}
Set<String> unique = new HashSet<>();
//save the short string array in hashset
for (String s: s2) {
unique.add(s);
}
//check the long string array and put the missing strings in result
for (String s: s1) {
if (!unique.contains(s)) {
res.add(s);
}
}
return res;
}
};
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。