Member-only story
LeetCode 5087: Letter Tile Possibilities in Swift
This is a scrabble challenge. What are the different combinations of letters that can be derived from a set of letters.
For example with input tiles of “AAB” there are the following possibilities:
“A”
“B”
“AA”
“AB”
“BA”
“AAB”
“ABA”
“BAA”
Note that there are no repetitions for the tile possibilities here!
Dictionaries
Dictionaries in Swift typically have a lookup of O(1). This makes it a good candidate for this problem.
The setup
We can place each of our tiles into a frequency array, and can use the great initializer in Swift that allows us to give a default for each tile (making the syntax easy to read):
The algorithm
This algorithm for the input “AAB” is as follows: