Skip to content

Commit d896a22

Browse files
solves count common words with only one occurrence
1 parent fc6211f commit d896a22

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,8 @@
493493
| 2062 | [Count Vowel Substrings of a String](https://leetcode.com/problems/count-vowel-substrings-of-a-string) | [![Java](assets/java.png)](src/CountVowelSubstringsOfAString.java) | |
494494
| 2068 | [Check Whether Two Strings are Almost Equivalent](https://leetcode.com/problems/check-whether-two-strings-are-almost-equivalent) | [![Java](assets/java.png)](src/CheckWhetherTwoStringsAreAlmostEquivalent.java) | |
495495
| 2073 | [Time Needed to Buy Tickets](https://leetcode.com/problems/time-needed-to-buy-tickets) | [![Java](assets/java.png)](src/TimeNeededToBuyTickets.java) | |
496-
| 2078 | [Two Furthest Houses With Different Colors](https://leetcode.com/problems/two-furthest-houses-with-different-colors) | [![Java](assets/java.png)](src/TwoFurthestHousesWithDifferentColors.java) | |
497-
| 2085 | [Count Common Words With One Occurrence](https://leetcode.com/problems/count-common-words-with-one-occurrence) | | |
496+
| 2078 | [Two Furthest Houses With Different Colors](https://leetcode.com/problems/two-furthest-houses-with-different-colors) | [![Java](assets/java.png)](src/TwoFurthestHousesWithDifferentColors.java) | |
497+
| 2085 | [Count Common Words With One Occurrence](https://leetcode.com/problems/count-common-words-with-one-occurrence) | [![Java](assets/java.png)](src/CountCommonWordsWithOneOccurrence.java) | |
498498
| 2089 | [Find Target Indices After Sorting Array](https://leetcode.com/problems/find-target-indices-after-sorting-array) | | |
499499
| 2094 | [Finding 3-Digit Even Numbers](https://leetcode.com/problems/finding-3-digit-even-numbers) | | |
500500
| 2099 | [Find Subsequence of Length K With the Largest Sum](https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum) | | |

Diff for: src/CountCommonWordsWithOneOccurrence.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://leetcode.com/problems/count-common-words-with-one-occurrence
2+
// T: O(|words1| + |words2|)
3+
// S: O(|words1| + |words2|)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class CountCommonWordsWithOneOccurrence {
9+
public int countWords(String[] words1, String[] words2) {
10+
final Map<String, Integer> words1Frequency = getFrequencies(words1);
11+
final Map<String, Integer> words2Frequency = getFrequencies(words2);
12+
int words = 0;
13+
for (Map.Entry<String, Integer> entry: words1Frequency.entrySet()) {
14+
if (entry.getValue() == 1 && words2Frequency.getOrDefault(entry.getKey(), 0) == 1) {
15+
words++;
16+
}
17+
}
18+
return words;
19+
}
20+
21+
private Map<String, Integer> getFrequencies(String[] array) {
22+
final Map<String, Integer> frequencies = new HashMap<>();
23+
for (String s : array) {
24+
frequencies.put(s, frequencies.getOrDefault(s, 0) + 1);
25+
}
26+
return frequencies;
27+
}
28+
}

0 commit comments

Comments
 (0)