You haveĀ nĀ Ā tiles, where each tile has one letterĀ tiles[i]Ā printed on it.

ReturnĀ the number of possible non-empty sequences of lettersĀ you can make using the letters printed on thoseĀ tiles.

solution

We use a depth-first search to enumerate all the possibilities. To avoid duplicates and infinite loops, we use a used set to keep track of which indices we’ve already used at each step of the DFS.

def numTilePossibilities(self, tiles: str) -> int:
	used = set()
	sequences = set()
 
	def dfs(acc):
		if len(acc) > len(tiles):
			return if len(acc) > 0 and acc not in sequences:
			sequences.add(acc)
 
		for i in range(len(tiles)):
			if i not in used:
				used.add(i)
				dfs(acc+tiles[i])
				used.remove(i)
				
	dfs("")
	return len(sequences)