- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution884.java
More file actions
Latest commit
executable file
·37 lines (31 loc) · 1.02 KB
/
Copy pathSolution884.java
File metadata and controls
executable file
·37 lines (31 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.HashMap;
importjava.util.List;
publicclassSolution884 {
publicString[] uncommonFromSentences(StringA, StringB) {
StringunionSentence = A +" "+ B;
String[] unionSentenceList = unionSentence.split(" ");
HashMap<String,Integer> hashMap = newHashMap<>();
List<String> ans = newArrayList<>();
for (Stringitem:unionSentenceList) {
if (hashMap.containsKey(item)){
hashMap.put(item,2);
}else {
hashMap.put(item,1);
}
}
for (Stringkey:hashMap.keySet()){
if (hashMap.get(key)==1) {
ans.add(key);
}
}
returnans.toArray(newString[ans.size()]);
}
publicstaticvoidmain(String[] args) {
Solution884s = newSolution884();
StringA = "apple apple";
StringB = "banana";
System.out.println(Arrays.toString(s.uncommonFromSentences(A,B)));
}
}