-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
50 lines (42 loc) · 1.66 KB
/
Solution.cs
File metadata and controls
50 lines (42 loc) · 1.66 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
using System.Globalization;
using System.Text;
namespace LeetCode.Problem1417{
//1417. Reformat The String
//https://leetcode.com/problems/reformat-the-string/
/*
You are given an alphanumeric string s. (Alphanumeric string is a string consisting of lowercase English letters and digits).
You have to find a permutation of the string where no letter is followed by another letter and no digit is followed
by another digit. That is, no two adjacent characters have the same type.
Return the reformatted string or return an empty string if it is impossible to reformat the string.
*/
public class Solution {
public string Reformat(string s) {
var countLetter = s.Where(p => Convert.ToInt32(p) >= 97).Count();
var countDigit = s.Where(p => Convert.ToInt32(p) < 97).Count();
if (Math.Abs(countLetter - countDigit) > 1)
return "";
else{
Queue<char> Digits = new Queue<char>(s.Where(p => Convert.ToInt32(p) < 97).Select(p=>p).ToArray());
Queue<char> Letters = new Queue<char>(s.Where(p => Convert.ToInt32(p) >= 97).Select(p=>p).ToArray());
String ans = string.Empty;
if (Digits.Count == Letters.Count){
while (Digits.Count > 0)
ans = ans + Digits.Dequeue() + Letters.Dequeue();
}
else {
if (Digits.Count < Letters.Count){
ans= Letters.Dequeue().ToString();
while (Digits.Count > 0)
ans = ans + Digits.Dequeue() + Letters.Dequeue();
}
else{
ans = Digits.Dequeue().ToString();
while (Digits.Count > 0)
ans = ans + Letters.Dequeue() + Digits.Dequeue();
}
}
return ans;
}
}
}
}