Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given two string arrays words1 and words2, return the number of strings that appear exactly once in each of the two arrays.
Example 1:
Input: words1 = ["leetcode","is","amazing","as","is"], words2 = ["amazing","leetcode","is"] Output: 2 Explanation: - "leetcode" appears exactly once in each of the two arrays. We count this string. - "amazing" appears exactly once in each of the two arrays. We count this string. - "is" appears in each of the two arrays, but there are 2 occurrences of it in words1. We do not count this string. - "as" appears once in words1, but does not appear in words2. We do not count this string. Thus, there are 2 strings that appear exactly once in each of the two arrays.
Example 2:
Input: words1 = ["b","bb","bbb"], words2 = ["a","aa","aaa"] Output: 0 Explanation: There are no strings that appear in each of the two arrays.
Example 3:
Input: words1 = ["a","ab"], words2 = ["a","a","a","ab"] Output: 1 Explanation: The only string that appears exactly once in each of the two arrays is "ab".
Constraints:
1 <= words1.length, words2.length <= 10001 <= words1[i].length, words2[j].length <= 30words1[i] and words2[j] consists only of lowercase English letters.Problem summary: Given two string arrays words1 and words2, return the number of strings that appear exactly once in each of the two arrays.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
["leetcode","is","amazing","as","is"] ["amazing","leetcode","is"]
["b","bb","bbb"] ["a","aa","aaa"]
["a","ab"] ["a","a","a","ab"]
intersection-of-two-arrays)uncommon-words-from-two-sentences)kth-distinct-string-in-an-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2085: Count Common Words With One Occurrence
class Solution {
public int countWords(String[] words1, String[] words2) {
Map<String, Integer> cnt1 = new HashMap<>();
Map<String, Integer> cnt2 = new HashMap<>();
for (var w : words1) {
cnt1.merge(w, 1, Integer::sum);
}
for (var w : words2) {
cnt2.merge(w, 1, Integer::sum);
}
int ans = 0;
for (var e : cnt1.entrySet()) {
if (e.getValue() == 1 && cnt2.getOrDefault(e.getKey(), 0) == 1) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2085: Count Common Words With One Occurrence
func countWords(words1 []string, words2 []string) (ans int) {
cnt1 := map[string]int{}
cnt2 := map[string]int{}
for _, w := range words1 {
cnt1[w]++
}
for _, w := range words2 {
cnt2[w]++
}
for w, v := range cnt1 {
if v == 1 && cnt2[w] == 1 {
ans++
}
}
return
}
# Accepted solution for LeetCode #2085: Count Common Words With One Occurrence
class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
cnt1 = Counter(words1)
cnt2 = Counter(words2)
return sum(v == 1 and cnt2[w] == 1 for w, v in cnt1.items())
// Accepted solution for LeetCode #2085: Count Common Words With One Occurrence
struct Solution;
use std::collections::HashMap;
impl Solution {
fn count_words(words1: Vec<String>, words2: Vec<String>) -> i32 {
let mut hm1: HashMap<String, usize> = HashMap::new();
let mut hm2: HashMap<String, usize> = HashMap::new();
for w in words1 {
*hm1.entry(w).or_default() += 1;
}
for w in words2 {
*hm2.entry(w).or_default() += 1;
}
let mut res = 0;
for (k, v) in hm1 {
if v == 1 && *hm2.get(&k).unwrap_or(&0) == 1 {
res += 1;
}
}
res
}
}
#[test]
fn test() {
let words1 = vec_string!["leetcode", "is", "amazing", "as", "is"];
let words2 = vec_string!["amazing", "leetcode", "is"];
let res = 2;
assert_eq!(Solution::count_words(words1, words2), res);
let words1 = vec_string!["b", "bb", "bbb"];
let words2 = vec_string!["a", "aa", "aaa"];
let res = 0;
assert_eq!(Solution::count_words(words1, words2), res);
let words1 = vec_string!["a", "ab"];
let words2 = vec_string!["a", "a", "a", "ab"];
let res = 1;
assert_eq!(Solution::count_words(words1, words2), res);
}
// Accepted solution for LeetCode #2085: Count Common Words With One Occurrence
function countWords(words1: string[], words2: string[]): number {
const cnt1 = new Map<string, number>();
const cnt2 = new Map<string, number>();
for (const w of words1) {
cnt1.set(w, (cnt1.get(w) ?? 0) + 1);
}
for (const w of words2) {
cnt2.set(w, (cnt2.get(w) ?? 0) + 1);
}
let ans = 0;
for (const [w, v] of cnt1) {
if (v === 1 && cnt2.get(w) === 1) {
++ans;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.