File tree Expand file tree Collapse file tree 1 file changed +35
-1
lines changed
Expand file tree Collapse file tree 1 file changed +35
-1
lines changed Original file line number Diff line number Diff line change 33 *
44 * [791] 自定义字符串排序
55 */
6+ #include < map>
7+ #include < set>
8+ #include < sstream>
9+ using namespace std ;
610
711// @lc code=start
812class Solution {
913public:
1014 string customSortString (string order, string s) {
11-
15+ map<int , char > ms;
16+ map<char , int > mss;
17+ set<char > ss;
18+ stringstream cs;
19+ stringstream cs1;
20+ for (size_t i = 0 ; i < order.size (); i++) {
21+ ss.insert (order[i]);
22+ ms.insert (make_pair (i, order[i]));
23+ }
24+
25+ for (size_t i = 0 ; i < s.size (); i++) {
26+ if (ss.count (s[i]) == 0 ) {
27+ cs << s[i];
28+ continue ;
29+ }
30+ if (mss.count (s[i]) > 0 ) {
31+ mss[s[i]] = mss[s[i]] + 1 ;
32+ } else {
33+ mss.insert (make_pair (s[i], 1 ));
34+ }
35+ }
36+
37+ for (size_t i = 0 ; i < order.size (); i++) {
38+ if (mss.count (ms[i]) > 0 ) {
39+ for (size_t k = 0 ; k < mss[ms[i]]; k++) {
40+ cs1 << ms[i];
41+ }
42+ }
43+ }
44+
45+ return cs1.str () + cs.str ();
1246 }
1347};
1448// @lc code=end
You can’t perform that action at this time.
0 commit comments