# 1079. Letter Tile Possibilities 活字印刷

@TOC

## # 题目描述

You have a set of 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.

Example 1:

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

Example 2:

``````Input: "AAABBC"
Output: 188
``````

Note:

1. `1 <= tiles.length <= 7`
2. tiles consists of uppercase English letters.

## # 解题方法

### # 回溯

``````A           // 剩余A、B
AA AB       // 剩余B，剩余A
AAB ABA     // 不剩

B           // 剩余A、A
BA          // 剩余A
BAA         // 不剩
``````

C++代码如下：

``````class Solution {
public:
int numTilePossibilities(string tiles) {
vector<int> count(26, 0);
for (char c : tiles) {
count[c - 'A'] ++;
}
int res = 0;
backtrack(count, res);
return res;
}
void backtrack(vector<int>& count, int& res) {
for (int i = 0; i < 26; ++i) {
if (count[i] == 0) continue;
res ++;
count[i] --;
backtrack(count, res);
count[i] ++;
}
}
};
``````

## # 日期

2019 年 9 月 25 日 —— 做梦都在秋招，这个秋天有毒