-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1370-IncreasingDecreasingString.cs
40 lines (35 loc) · 1.1 KB
/
1370-IncreasingDecreasingString.cs
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
//-----------------------------------------------------------------------------
// Runtime: 88ms
// Memory Usage: 24.9 MB
// Link: https://leetcode.com/submissions/detail/327819541/
//-----------------------------------------------------------------------------
using System.Text;
namespace LeetCode
{
public class _1370_IncreasingDecreasingString
{
public string SortString(string s)
{
var counts = new int[26];
foreach (var ch in s)
counts[ch - 'a']++;
var sb = new StringBuilder();
while (sb.Length < s.Length)
{
for (int i = 0; i < 26; i++)
if (counts[i] > 0)
{
counts[i]--;
sb.Append((char)('a' + i));
}
for (int i = 25; i >= 0; i--)
if (counts[i] > 0)
{
counts[i]--;
sb.Append((char)('a' + i));
}
}
return sb.ToString();
}
}
}