1079. Letter Tile Possibilities ⭐

Medium

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.

Example 1:

Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".

Example 2:

Input: tiles = "AAABBC"
Output: 188

Example 3:

Input: tiles = "V"
Output: 1

Constraints:

  • 1 <= tiles.length <= 7

  • tiles consists of uppercase English letters.

解題

Runtime:0 ms, faster than 100%

Memory Usage: 1.9 MB, less than 87.5%

func numTilePossibilities(tiles string) int {
    count := make([]int, 26)
    for i:=0; i<len(tiles); i++ {
        count[tiles[i] - 'A']++
    }

    var backtracking func() int
    backtracking = func() int {
        res := 0
        for i:=0; i<26; i++ {
            if count[i] == 0 { continue }
            res++
            count[i]--
            res += backtracking()
            count[i]++
        }

        return res
    }

    return backtracking()
}

Last updated