Skip to content

Commit 57a924d

Browse files
authored
Update letter-tile-possibilities.cpp
1 parent 5735ba0 commit 57a924d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

C++/letter-tile-possibilities.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,30 @@ class Solution {
4646
return result;
4747
}
4848
};
49+
50+
// Time: O(r), r is the value of result
51+
// Space: O(n)
52+
class Solution2 {
53+
public:
54+
int numTilePossibilities(string tiles) {
55+
unordered_map<char,int> count;
56+
for (const auto& c : tiles) {
57+
++count[c];
58+
}
59+
return dfs(&count);
60+
}
61+
62+
private:
63+
int dfs(unordered_map<char,int> *count) {
64+
int total = 0;
65+
for (auto& c : *count) {
66+
if (c.second) {
67+
--c.second;
68+
total += 1 + dfs(count);
69+
++c.second;
70+
}
71+
}
72+
return total;
73+
}
74+
};
75+

0 commit comments

Comments
 (0)