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 a string array words, return an array of all characters that show up in all strings within the words (including duplicates). You may return the answer in any order.
Example 1:
Input: words = ["bella","label","roller"] Output: ["e","l","l"]
Example 2:
Input: words = ["cool","lock","cook"] Output: ["c","o"]
Constraints:
1 <= words.length <= 1001 <= words[i].length <= 100words[i] consists of lowercase English letters.Problem summary: Given a string array words, return an array of all characters that show up in all strings within the words (including duplicates). You may return the answer in any order.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
["bella","label","roller"]
["cool","lock","cook"]
intersection-of-two-arrays-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1002: Find Common Characters
class Solution {
public List<String> commonChars(String[] words) {
int[] cnt = new int[26];
Arrays.fill(cnt, 20000);
for (var w : words) {
int[] t = new int[26];
for (int i = 0; i < w.length(); ++i) {
++t[w.charAt(i) - 'a'];
}
for (int i = 0; i < 26; ++i) {
cnt[i] = Math.min(cnt[i], t[i]);
}
}
List<String> ans = new ArrayList<>();
for (int i = 0; i < 26; ++i) {
ans.addAll(Collections.nCopies(cnt[i], String.valueOf((char) ('a' + i))));
}
return ans;
}
}
// Accepted solution for LeetCode #1002: Find Common Characters
func commonChars(words []string) (ans []string) {
cnt := make([]int, 26)
for i := range cnt {
cnt[i] = 20000
}
for _, w := range words {
t := make([]int, 26)
for _, c := range w {
t[c-'a']++
}
for i := 0; i < 26; i++ {
cnt[i] = min(cnt[i], t[i])
}
}
for i := 0; i < 26; i++ {
for j := 0; j < cnt[i]; j++ {
ans = append(ans, string('a'+rune(i)))
}
}
return ans
}
# Accepted solution for LeetCode #1002: Find Common Characters
class Solution:
def commonChars(self, words: List[str]) -> List[str]:
cnt = Counter(words[0])
for w in words:
t = Counter(w)
for c in cnt:
cnt[c] = min(cnt[c], t[c])
return list(cnt.elements())
// Accepted solution for LeetCode #1002: Find Common Characters
struct Solution;
use std::usize;
impl Solution {
fn common_chars(a: Vec<String>) -> Vec<String> {
let n = a.len();
let mut counts: Vec<Vec<usize>> = vec![vec![0; 256]; n];
for i in 0..n {
let w = &a[i];
for c in w.chars() {
counts[i][c as usize] += 1;
}
}
let mut res: Vec<String> = vec![];
for i in 0..26 {
let c: u8 = b'a' + i;
let mut min = usize::MAX;
for j in 0..n {
let count = counts[j][c as usize];
min = usize::min(count, min);
}
for _ in 0..min {
res.push(format!("{}", c as char))
}
}
res
}
}
#[test]
fn test() {
let a: Vec<String> = vec_string!["bella", "label", "roller"];
let b: Vec<String> = vec_string!["e", "l", "l"];
assert_eq!(Solution::common_chars(a), b);
let a: Vec<String> = vec_string!["cool", "lock", "cook"];
let b: Vec<String> = vec_string!["c", "o"];
assert_eq!(Solution::common_chars(a), b);
}
// Accepted solution for LeetCode #1002: Find Common Characters
function commonChars(words: string[]): string[] {
const cnt = Array(26).fill(20000);
const aCode = 'a'.charCodeAt(0);
for (const w of words) {
const t = Array(26).fill(0);
for (const c of w) {
t[c.charCodeAt(0) - aCode]++;
}
for (let i = 0; i < 26; i++) {
cnt[i] = Math.min(cnt[i], t[i]);
}
}
const ans: string[] = [];
for (let i = 0; i < 26; i++) {
cnt[i] && ans.push(...String.fromCharCode(i + aCode).repeat(cnt[i]));
}
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.