-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
40 lines (39 loc) · 1.08 KB
/
Program.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
using System;
namespace IncreasingDecreasingString
{
class Program
{
static void Main(string[] args)
{
string s = "leetcode";
Console.WriteLine(SortString(s));
}
static string SortString(string s)
{
var record = new int[26];
for (int i = 0; i < s.Length; i++)
record[s[i] - 'a']++;
var res = "";
while (res.Length < s.Length)
{
for (int i = 0; i < record.Length && res.Length < s.Length; i++)
{
if (record[i] != 0)
{
res += (char)(i + 97);
record[i]--;
}
}
for (int i = record.Length - 1; i >= 0 && res.Length < s.Length; i--)
{
if (record[i] != 0)
{
res += (char)(i + 97);
record[i]--;
}
}
}
return res;
}
}
}