Skip to content

Commit a7eaedd

Browse files
solves uncommon words from two sentences
1 parent abc295e commit a7eaedd

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-195/2081-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-195/2081-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-206/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-206/2081-1abc9c.svg)
55
![problems-solved-python](https://img.shields.io/badge/Python-186/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
@@ -238,7 +238,7 @@
238238
| 874 | [Walking Robot Simulation](https://leetcode.com/problems/walking-robot-simulation) | |
239239
| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list) | [![Java](assets/java.png)](src/MiddleOfTheLinkedList.java) |
240240
| 883 | [Projection Area of 3D Shapes](https://leetcode.com/problems/projection-area-of-3d-shapes) | [![Java](assets/java.png)](src/ProjectionAreaOf3DShapes.java) |
241-
| 884 | [Uncommon Words from 2 Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences) | |
241+
| 884 | [Uncommon Words from 2 Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences) | [![Java](assets/java.png)](src/UncommonWordsFromTwoSentences.java) |
242242
| 888 | [Fair Candy Swap](https://leetcode.com/problems/fair-candy-swap) | |
243243
| 892 | [Surface Area of 3D Shapes](https://leetcode.com/problems/surface-area-of-3d-shapes) | |
244244
| 893 | [Groups of Special Equivalent Strings](https://leetcode.com/problems/groups-of-special-equivalent-strings) | |
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
public class UncommonWordsFromTwoSentences {
7+
public String[] uncommonFromSentences(String s1, String s2) {
8+
final Map<String, Integer> sentence1Words = getWordFrequencies(s1);
9+
final Map<String, Integer> sentence2Words = getWordFrequencies(s2);
10+
final List<String> result = new ArrayList<>();
11+
for (Map.Entry<String, Integer> entry : sentence1Words.entrySet()) {
12+
if (entry.getValue() == 1 && !sentence2Words.containsKey(entry.getKey())) result.add(entry.getKey());
13+
}
14+
for (Map.Entry<String, Integer> entry : sentence2Words.entrySet()) {
15+
if (entry.getValue() == 1 && !sentence1Words.containsKey(entry.getKey())) result.add(entry.getKey());
16+
}
17+
return result.toArray(new String[] {});
18+
}
19+
20+
private Map<String, Integer> getWordFrequencies(String string) {
21+
final Map<String, Integer> map = new HashMap<>();
22+
StringBuilder word =new StringBuilder();
23+
String s;
24+
for (int index = 0 ; index < string.length() ; index++) {
25+
if (string.charAt(index) == ' ') {
26+
s = word.toString();
27+
map.put(s, map.getOrDefault(s, 0) + 1);
28+
word = new StringBuilder();
29+
} else word.append(string.charAt(index));
30+
}
31+
s = word.toString();
32+
map.put(s, map.getOrDefault(s, 0) + 1);
33+
return map;
34+
}
35+
}

0 commit comments

Comments
 (0)