Ctrl K

Words From Letter Tiles

Easy
You have a set of letter tiles written as the string tiles: every character is one tile. You are also given a list of words words. A word can be spelled if each of its letters gets a tile of its own: a single tile cannot be used twice within one word. Each word is spelled separately, starting from the full set of tiles. Return the total length of all words in words that can be spelled.
Example 1
words=['team', 'meat', 'mate', 'tame', 'steam'], tiles='mteab' → 16

The tiles m, t, e, a, b spell "team", "meat", "mate" and "tame" (4 · 4 = 16 letters); "steam" needs an "s" we do not have.

Example 2
words=['pool', 'lop', 'loop'], tiles='olp' → 3

"pool" and "loop" each need two o's, but there is only one "o" tile. Only "lop" fits, so the answer is 3.

Constraints
  • 1 ≤ words.length ≤ 1000
  • 1 ≤ words[i].length ≤ 100
  • 1 ≤ tiles.length ≤ 100
  • words[i] and tiles consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWords From Working Keys EasyThe k-th Word With No Repeats EasyWords Listed Once in Both EasyPairs With the Same Letter Set EasyMost Searched Words Medium
loading Python…
Input
words=['team', 'meat', 'mate', 'tame', 'steam'], tiles='mteab'
Expected
16

Run checks the first 3 tests in your browser. Submit checks all 134 tests on the server, hidden ones included.