The tiles on the rack
- O(n²) · Very hard
- Full plan
- Python
- JavaScript
- loops
- lists
- strings
Problem
Half a minute of sand is left in the timer. Keoni looks at the tiles on his rack and runs through the words he knows: before he moves, he wants to count how many of them he could lay on the board with what he has. Write a function that takes tiles, a string of lowercase letters with one letter per tile, and words, a list of lowercase strings (either one may be empty). A word can be built if none of its letters appears in it more times than in tiles, because each tile is used only once. Each word is checked on its own, with the full rack: tiles are not used up from one word to the next. The empty word can always be built, and if a word is repeated in the list, it counts every time.
Return, as an integer, how many words in the list can be built.
With "gaakll" and ["gak", "lll", "kala", "aag"]: "gak" needs one g, one a and one k, and they are there; "kala" needs two a, one k and one l, and those are there too; "aag" needs two a and one g, also there; "lll" needs three l and there are only two. Three can be built, so it returns 3.
Examples
The example
"gaakll", ["gak", "lll", "kala", "aag"] → 3
Exactly the tiles it needs
"aab", ["aab", "aaab", "baa"] → 2
A repeated word counts twice
"team", ["meat", "meat", "meet"] → 2
Empty rack
"", ["", "a"] → 1
He knows no words
"abc", [] → 0
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def count_buildable(tiles, words):
passJavaScript
function countBuildable(tiles, words) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.