|
| 1 | +// Runtime: 126 ms (Top 73.02%) | Memory: 66.40 MB (Top 12.7%) |
| 2 | + |
1 | 3 | class Solution {
|
2 |
| - |
3 |
| - unordered_map<string, int> next_index_; |
4 |
| - |
5 |
| - bool contain(const string& fn) { |
6 |
| - return next_index_.find(fn) != next_index_.end(); |
7 |
| - } |
8 |
| - |
9 |
| - string get_name(string fn) { |
10 |
| - if (!contain(fn)) { |
11 |
| - next_index_.insert({fn, 1}); |
12 |
| - return fn; |
13 |
| - } |
14 |
| - |
15 |
| - int idx = next_index_[fn]; |
16 |
| - auto cur = fn; |
17 |
| - while (contain(cur)) { |
18 |
| - next_index_.insert({cur, 1}); |
19 |
| - cur = fn + "(" + to_string(idx) + ")"; |
20 |
| - ++idx; |
21 |
| - } |
22 |
| - next_index_.insert({cur, 1}); |
23 |
| - next_index_[fn] = idx; |
24 |
| - return cur; |
25 |
| - } |
26 |
| - |
27 | 4 | public:
|
28 | 5 | vector<string> getFolderNames(vector<string>& names) {
|
| 6 | + |
| 7 | + // You can understand the code by seeing explanation below |
| 8 | + |
| 9 | + unordered_map<string, int> mp; |
29 | 10 | vector<string> res;
|
30 |
| - for (auto& n : names) { |
31 |
| - res.push_back(get_name(n)); |
32 |
| - } |
33 | 11 |
|
| 12 | + for(auto i : names){ |
| 13 | + string val = i; |
| 14 | + int cnt = mp[val]; |
| 15 | + |
| 16 | + while(mp[val] != 0){ |
| 17 | + val = i + '(' + to_string(cnt) + ')'; |
| 18 | + cnt++; |
| 19 | + mp[i] = cnt; |
| 20 | + |
| 21 | + } |
| 22 | + mp[val]++; |
| 23 | + res.emplace_back(val); |
| 24 | + } |
34 | 25 | return res;
|
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + // For better Understanding Code explained Line by line |
| 30 | + |
| 31 | + // names = ["d", "d(1)", "d(2)", "d"] |
| 32 | + |
| 33 | + |
| 34 | + // running for loop |
| 35 | + // val = "d"; |
| 36 | + // cnt = mp[val] = mp["d"] = 0; |
| 37 | + // mp[val] = 0, so while loop not executed |
| 38 | + // mp[val]++ -> mp["d"] = 0+1 = 1; |
| 39 | + // res = ["d"] |
| 40 | + |
| 41 | + // Same for "d(1)" |
| 42 | + // mp["d(1)"] = 0+1 = 1; |
| 43 | + // res = ["d", "d(1)"] |
| 44 | + |
| 45 | + // Same for "d(2)" |
| 46 | + // mp["d(2)"] = 0+1 = 1; |
| 47 | + // res = ["d", "d(1)", "d(2)"] |
| 48 | + |
| 49 | + // now, i = "d", val = "d"; |
| 50 | + // cnt = mp[val] = mp["d"] = 1, (appeared before) |
| 51 | + // while loop executed bcoz mp[val] = 1 which is greater than 0 |
| 52 | + // In while loop, |
| 53 | + // cnt = 1+1 = 2; |
| 54 | + // val = i + '(' + to_string(cnt) + ')' = "d(2)" |
| 55 | + // mp[i] = mp["d"] = cnt = 2; |
| 56 | + |
| 57 | + // now mp[val] = mp["d(2)"] = 1, (appeared before) |
| 58 | + // while loop executed bcoz mp[val] = 1 which is greater than 0 |
| 59 | + // In while loop, |
| 60 | + // cnt = 2+1 = 3; |
| 61 | + // val = i + '(' + to_string(cnt) + ')' = "d(3)" |
| 62 | + // mp[i] = mp["d"] = cnt = 3; |
| 63 | + |
| 64 | + // now mp[val] = mp["d(3)"] = 0 |
| 65 | + // while loop closed |
| 66 | + // mp[val]++ = mp["d(3)"] = 0+1 = 1; |
| 67 | + // res = ["d", "d(1)", "d(2)", "d(3)"] |
| 68 | + |
| 69 | + // now, return res; |
35 | 70 | }
|
36 | 71 | };
|
0 commit comments