-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0791-Custom-sort-string.cs
More file actions
52 lines (43 loc) · 1.22 KB
/
0791-Custom-sort-string.cs
File metadata and controls
52 lines (43 loc) · 1.22 KB
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
49
50
51
52
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0791.Custom_sort_string
{
public class _0791_Custom_sort_string
{
public string CustomSortString(string order, string str)
{
if (string.IsNullOrEmpty(order) || string.IsNullOrEmpty(str)) return string.Empty;
Dictionary<char, int> dic = new Dictionary<char, int>();
foreach (char c in str)
{
if (!dic.ContainsKey(c))
dic.Add(c, 0);
dic[c]++;
}
StringBuilder sb = new StringBuilder();
foreach (char c in order)
{
if (dic.ContainsKey(c))
{
while (dic[c] > 0)
{
sb.Append(c);
dic[c]--;
}
}
dic.Remove(c);
}
foreach (char key in dic.Keys)
{
int count = dic[key];
while (count > 0)
{
sb.Append(key);
count--;
}
}
return sb.ToString();
}
}
}