Find the characters common to every word, counting repeats

Quick Overview

Given a list of lowercase words, return every character that appears in all of them, repeated as many times as it appears in each word, in alphabetical order. It tests frequency counting with fixed-size arrays or hash maps and handling repeated letters correctly.

Find the characters common to every word, counting repeats

Company: Tripadvisor

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string array `words`, return all characters that show up in every string in `words`, including duplicates. If a character appears at least 3 times in every string but not at least 4 times in every string, include that character exactly three times in the answer. ### Function Signature ```python def common_chars(words: list[str]) -> list[str]: ... ``` ### Rules - Each element of the result is a one-character string. - Return the characters sorted in alphabetical order, so repeated characters sit next to each other. - Return an empty list when no character appears in every word. ### Constraints - `1 <= len(words) <= 100` - `1 <= len(words[i]) <= 100` - `words[i]` consists of lowercase English letters. ### Examples ```text Input: words = ["bella","label","roller"] Output: ["e","l","l"] ``` Every word has at least one "e" and at least two "l" characters. "b" and "a" are missing from "roller", and "r" and "o" are missing from the other two words. ```text Input: words = ["cool","lock","cook"] Output: ["c","o"] ``` "c" appears once in each word. "o" appears twice in "cool" and "cook" but only once in "lock", so it is included once. "l" is missing from "cook". ```text Input: words = ["abc","def"] Output: [] ``` The two words share no letters.

Overview: Given a list of lowercase words, return every character that appears in all of them, repeated as many times as it appears in each word, in alphabetical order. It tests frequency counting with fixed-size arrays or hash maps and handling repeated letters correctly.

Tripadvisor logo
Tripadvisor
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a string array words, return all characters that show up in every string in words, including duplicates. If a character appears at least 3 times in every string but not at least 4 times in every string, include that character exactly three times in the answer.

Function Signature

def common_chars(words: list[str]) -> list[str]:
    ...

Rules

  • Each element of the result is a one-character string.
  • Return the characters sorted in alphabetical order, so repeated characters sit next to each other.
  • Return an empty list when no character appears in every word.

Constraints

  • 1 <= len(words) <= 100
  • 1 <= len(words[i]) <= 100
  • words[i] consists of lowercase English letters.

Examples

Input: words = ["bella","label","roller"]
Output: ["e","l","l"]

Every word has at least one "e" and at least two "l" characters. "b" and "a" are missing from "roller", and "r" and "o" are missing from the other two words.

Input: words = ["cool","lock","cook"]
Output: ["c","o"]

"c" appears once in each word. "o" appears twice in "cool" and "cook" but only once in "lock", so it is included once. "l" is missing from "cook".

Input: words = ["abc","def"]
Output: []

The two words share no letters.