-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1079.cpp
More file actions
34 lines (25 loc) · 791 Bytes
/
1079.cpp
File metadata and controls
34 lines (25 loc) · 791 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<vector>
#include<unordered_map>
void backtrack(std::unordered_map<char, int>& freq, int& count, int length) {
for (auto& [ch, f] : freq) {
if (f > 0) { // If we still have this character left
count++; // Count this sequence
f--; // Use this character
backtrack(freq, count, length + 1);
f++; // Backtrack (restore character)
}
}
}
int numTilePossibilities(std::string tiles) {
std::unordered_map<char, int> freq;
for (char ch : tiles) freq[ch]++; // Count character frequencies
int count = 0;
backtrack(freq, count, 0);
return count;
}
int main() {
std::string tiles = "AAB";
std::cout << numTilePossibilities(tiles) << std::endl;
std::cin.get();
}