1079. Letter Tile Possibilities

Approach 1: Generate All permutations

class Solution:        
    def numTilePossibilities(self, tiles: str) -> int:
        ans = 0
        for i in range(1, len(tiles)+1):
            ans += len(set(itertools.permutations(tiles,i)))
        return ans

Use itertools to generate all the permutations of the given string, of all possible lengths and then add the number of unique strings in that list to get the answer

This solution although not the best comes in at 93.51% percentile at 48ms

Time Complexity: O(n!)

Space Complexity: O(n!) [since all strings are being stored, albeit temporarily]

Last updated