Show problems as
Classic problem #1002EasyArrayHash TableString

Find Common Characters

Real-world version: Tiles Every Rack Can Supply
Tracked in this browser only
Read Find Common Characters on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Each player in a word game holds a rack of letter tiles. racks[i] is a string listing the tiles on the i-th rack, one lowercase letter per tile.

The umpire wants the multiset of tiles that every rack can supply at the same time. Copies count: if a letter is on one rack three times and on another rack twice, then only two copies of it can come from every rack.

Return those tiles as a list of one-letter strings, one entry per copy. Any order is accepted. Return an empty list when no letter sits on every rack.

Examples

Example 1

Input
racks = ["ppqqr", "pqrst", "zpqq"]
Output
["p", "q"]

Letter p sits on all three racks once each, so one copy is available. Letter q sits twice, once and twice, so one copy is available. Letter r is missing from the third rack, and no other letter is on all three.

Example 2

Input
racks = ["eexz", "zex"]
Output
["e", "x", "z"]

The first rack has two e tiles but the second has only one, so one e can come from both. Letters x and z are on each rack once.

Example 3

Input
racks = ["hjk", "wxy"]
Output
[]

The two racks share no letter at all, so the answer is the empty list.

Constraints

  • 1 <= racks.length <= 100
  • 1 <= racks[i].length <= 100
  • racks[i] consists of lowercase English letters.

The values you return may be in any order.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def shared_tiles(racks: list[str]) -> list[str]:
Java
public List<String> sharedTiles(String[] racks)