-
Notifications
You must be signed in to change notification settings - Fork 359
/
s1.cpp
31 lines (31 loc) · 913 Bytes
/
s1.cpp
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
// OJ: https://leetcode.com/problems/making-file-names-unique/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
public:
vector<string> getFolderNames(vector<string>& names) {
int N = names.size();
vector<string> ans(N);
unordered_map<string, int> m;
for (int i = 0; i < N; ++i) {
auto &name = names[i];
if (m.count(name) == 0) {
ans[i] = name;
m[name] = 1;
} else {
int index = m[name];
while (true) {
auto n = name + "(" + to_string(index++) + ")";
if (m.count(n) == 0) {
ans[i] = n;
m[name] = index;
m[n] = 1;
break;
}
}
}
}
return ans;
}
};