Skip to content

Commit 59207ed

Browse files
authored
Create making-file-names-unique.cpp
1 parent c3153b1 commit 59207ed

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

C++/making-file-names-unique.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
vector<string> getFolderNames(vector<string>& names) {
7+
unordered_map<string, int> count;
8+
vector<string> result;
9+
unordered_set<string> lookup;
10+
for (const auto& name : names) {
11+
auto& c = count[name];
12+
string name_with_suffix;
13+
do {
14+
name_with_suffix = c ? name + "(" + to_string(c) + ")" : name;
15+
++c;
16+
} while (lookup.count(name_with_suffix));
17+
result.emplace_back(name_with_suffix);
18+
lookup.emplace(move(name_with_suffix));
19+
}
20+
return result;
21+
}
22+
};

0 commit comments

Comments
 (0)