1079. Letter Tile Possibilities

Source code notebook Author Update time

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.
# @lc code=start
using LeetCode

function letter_tile_possibilities(tiles::String)
    cnt = zeros(Int, 26)
    for c in tiles
        cnt[c - 'A' + 1] += 1
    end
    function dfs(cnt::Vector{Int})
        res = 0
        for i in 1:26
            cnt[i] == 0 && continue
            res += 1
            cnt[i] -= 1
            res += dfs(cnt)
            cnt[i] += 1
        end
        res
    end
    dfs(cnt)
end
# @lc code=end
letter_tile_possibilities (generic function with 1 method)

This page was generated using DemoCards.jl and Literate.jl.