LeetCode solutions

1160. Letter Tile Possibilities

My accepted Python solution to LeetCode problem 1160, Letter Tile Possibilities, running in 37ms.

  • Difficulty: Medium
  • Python
  • Runtime 37ms
  • Memory 17.5MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 37ms, memory 17.5MB, accepted 2026-01-02.

python
class Solution:
    def numTilePossibilities(self, tiles: str) -> int:
        from collections import Counter
        
        count = Counter(tiles)
        
        def backtrack():
            total = 0
            for c in count:
                if count[c] > 0:
                    # Use this character
                    total += 1
                    count[c] -= 1
                    # Continue building sequences
                    total += backtrack()
                    # Backtrack
                    count[c] += 1
            return total
        
        return backtrack()

Source