Meta Data#
Difficulty: easy First Attempt: 2022-09-27
- Submissions: Runtime Error → Accepted ×3
Approach#
Each character appears in the answer as many times as its minimum count over all words (Hash Table Template 1). Python’s Counter & Counter takes per-character minimums; intersect over all words and expand with elements().
My Attempts#
Attempt 1 intersected a set with a string (set & str, TypeError), and a set drops multiplicity anyway: ["bb"] should give two bs. Switching to Counter & passed.
Solution#
class Solution:
def commonChars(self, words: List[str]) -> List[str]:
common = Counter(words[0])
for w in words[1:]:
common &= Counter(w)
return list(common.elements())
Complexity#
Time O(total characters), space O(k).
Mistakes to Avoid#
setloses multiplicity:["bb"]gives["b", "b"].set & strmixes types and raises TypeError.
Interview Explanation#
I count each word and intersect the Counters, taking each character’s minimum count, then expand into a list. Time is linear in the total length.
