Skip to content

Commit 8532e72

Browse files
authored
Create custom-sort-string.cpp
1 parent 85e1299 commit 8532e72

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

C++/custom-sort-string.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
string customSortString(string S, string T) {
7+
unordered_set<char> s(S.cbegin(), S.cend());
8+
unordered_map<char, int> counter;
9+
for (const auto& c : T) {
10+
++counter[c];
11+
}
12+
string result;
13+
for (const auto& c : S) {
14+
result += string(counter[c], c);
15+
}
16+
for (const auto& kvp : counter) {
17+
if (!s.count(kvp.first)) {
18+
result += string(kvp.second, kvp.first);
19+
}
20+
}
21+
return result;
22+
}
23+
};

0 commit comments

Comments
 (0)