public static string ReorganizeString(string s) {
int count = s.GroupBy(c => c).Max(g => g.Count());
if (count > (s.Length + 1) / 2)
string st = string.Concat(s
.OrderByDescending(g => g.Count())
StringBuilder sb = new StringBuilder(s.Length);
for (int i = 0; i < s.Length / 2; ++i) {
sb.Append(st[(st.Length + 1) / 2 + i]);
sb.Append(st[st.Length / 2]);
public static void Main() {
string[] tests = new string[] {
string result = string.Join(Environment.NewLine, tests
.Select(test => $"{test,10} => {ReorganizeString(test)}"));