forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
48 lines (37 loc) · 987 Bytes
/
main.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/// Source : https://leetcode.com/problems/custom-sort-string/description/
/// Author : liuyubobobo
/// Time : 2018-02-24
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
/// Count and write
/// Time Complexity: O(len(S) + len(T))
/// Space Complexity: O(len(T))
class Solution {
public:
string customSortString(string S, string T) {
unordered_map<char, int> freq;
for(char c: T)
freq[c] ++;
string res = "";
for(char c: S){
int f = freq[c];
if(f)
for(int i = 0 ; i < f ; i ++)
res += c;
freq.erase(c);
}
for(const pair<char, int>& p: freq){
int f = p.second;
if(f)
for(int i = 0 ; i < f ; i ++)
res += p.first;
}
return res;
}
};
int main() {
cout << Solution().customSortString("cba", "abcd") << endl;
return 0;
}