|
5 | 5 | import java.util.List;
|
6 | 6 | import java.util.Map;
|
7 | 7 |
|
8 |
| -/** |
9 |
| - * 884. Uncommon Words from Two Sentences |
10 |
| - * |
11 |
| - * We are given two sentences A and B. (A sentence is a string of space separated words. Each word consists only of lowercase letters.) |
12 |
| - * |
13 |
| - * A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence. |
14 |
| - * |
15 |
| - * Return a list of all uncommon words. |
16 |
| - * |
17 |
| - * You may return the list in any order. |
18 |
| - * |
19 |
| - * |
20 |
| - * |
21 |
| - * Example 1: |
22 |
| - * |
23 |
| - * Input: A = "this apple is sweet", B = "this apple is sour" |
24 |
| - * Output: ["sweet","sour"] |
25 |
| - * Example 2: |
26 |
| - * |
27 |
| - * Input: A = "apple apple", B = "banana" |
28 |
| - * Output: ["banana"] |
29 |
| - * |
30 |
| - * |
31 |
| - * Note: |
32 |
| - * |
33 |
| - * 0 <= A.length <= 200 |
34 |
| - * 0 <= B.length <= 200 |
35 |
| - * A and B both contain only spaces and lowercase letters. |
36 |
| - */ |
37 | 8 | public class _884 {
|
38 |
| - public static class Solution1 { |
39 |
| - public String[] uncommonFromSentences(String A, String B) { |
40 |
| - Map<String, Integer> map = new HashMap<>(); |
41 |
| - for (String word : A.split(" ")) { |
42 |
| - map.put(word, map.getOrDefault(word, 0) + 1); |
43 |
| - } |
| 9 | + public static class Solution1 { |
| 10 | + public String[] uncommonFromSentences(String A, String B) { |
| 11 | + Map<String, Integer> map = new HashMap<>(); |
| 12 | + for (String word : A.split(" ")) { |
| 13 | + map.put(word, map.getOrDefault(word, 0) + 1); |
| 14 | + } |
44 | 15 |
|
45 |
| - for (String word : B.split(" ")) { |
46 |
| - map.put(word, map.getOrDefault(word, 0) + 1); |
47 |
| - } |
48 |
| - List<String> result = new ArrayList<>(); |
49 |
| - for (String key : map.keySet()) { |
50 |
| - if (map.get(key) == 1) { |
51 |
| - result.add(key); |
| 16 | + for (String word : B.split(" ")) { |
| 17 | + map.put(word, map.getOrDefault(word, 0) + 1); |
| 18 | + } |
| 19 | + List<String> result = new ArrayList<>(); |
| 20 | + for (String key : map.keySet()) { |
| 21 | + if (map.get(key) == 1) { |
| 22 | + result.add(key); |
| 23 | + } |
| 24 | + } |
| 25 | + String[] strs = new String[result.size()]; |
| 26 | + result.toArray(strs); |
| 27 | + return strs; |
52 | 28 | }
|
53 |
| - } |
54 |
| - String[] strs = new String[result.size()]; |
55 |
| - result.toArray(strs); |
56 |
| - return strs; |
57 | 29 | }
|
58 |
| - } |
59 | 30 | }
|
0 commit comments