Skip to content

Commit a1435a2

Browse files
2 parents 1a1d073 + 147f3bb commit a1435a2

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Your ideas/fixes/algorithms are more than welcome!
3030
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
3131
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
3232
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java) | O(n) | O(1) | |Easy|
33+
|884|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_884.java) | O(n) | O(k) | |Easy|
3334
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_844.java) | O(n) | O(1) | |Easy|
3435
|832|[Flipping an Image](https://leetcode.com/problems/flipping-an-image/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_832.java) | O(n) | O(1) | |Easy|
3536
|830|[Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_830.java) | O(n) | O(n) | |Easy|
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
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+
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+
}
44+
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);
52+
}
53+
}
54+
String[] strs = new String[result.size()];
55+
result.toArray(strs);
56+
return strs;
57+
}
58+
}
59+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._884;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _884Test {
10+
private static _884.Solution1 solution1;
11+
private static String[] expected;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _884.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
expected = new String[] {"sweet", "sour"};
21+
assertArrayEquals(expected,
22+
solution1.uncommonFromSentences("this apple is sweet", "this apple is sour"));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
expected = new String[] {"banana"};
28+
assertArrayEquals(expected,
29+
solution1.uncommonFromSentences("apple apple", "banana"));
30+
}
31+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy