classSolution { public: intnumTilePossibilities(string tiles){ unordered_map<char, int> count; set<char> tile; int n = tiles.length(); for (char c : tiles) { count[c]++; tile.insert(c); } returndfs(count, tile, n) - 1; }
intdfs(unordered_map<char, int>& count, set<char>& tile, int i){ if (i == 0) { return1; } int res = 1; for (char t : tile) { if (count[t] > 0) { count[t]--; res += dfs(count, tile, i - 1); count[t]++; } } return res; } };
funcnumTilePossibilities(tiles string)int { count := make(map[rune]int) for _, t := range tiles { count[t]++ } return dfs(len(tiles), count) - 1 }
funcdfs(i int, count map[rune]int)int { if i == 0 { return1 } res := 1 for t := range count { if count[t] > 0 { count[t]-- res += dfs(i - 1, count) count[t]++ } } return res }
var numTilePossibilities = function(tiles) { const count = newMap() for (let t of tiles) { count.set(t, (count.get(t) || 0) + 1) } const tile = newSet(tiles) const n = tiles.length
functiondfs(i) { if (i == n) { return1 } let res = 1 for (let t of tile) { if (count.get(t) > 0) { count.set(t, count.get(t) - 1) res += dfs(i + 1) count.set(t, count.get(t) + 1) } } return res }
publicclassSolution { publicintNumTilePossibilities(string tiles) { IDictionary<char, int> count = new Dictionary<char, int>(); foreach (char t in tiles) { if (count.ContainsKey(t)) { count[t]++; } else { count.Add(t, 1); } } ISet<char> tile = new HashSet<char>(count.Keys); return DFS(tiles.Length, count, tile) - 1; }
privateintDFS(int i, IDictionary<char, int> count, ISet<char> tile) { if (i == 0) { return1; } int res = 1; foreach (char t in tile) { if (count[t] > 0) { count[t]--; res += DFS(i - 1, count, tile); count[t]++; } } return res; } }