forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1417.java
More file actions
21 lines (19 loc) · 711 Bytes
/
1417.java
File metadata and controls
21 lines (19 loc) · 711 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public String reformat(String s) {
Queue<Character> digits = new ArrayDeque();
Queue<Character> chars = new ArrayDeque();
for(char c : s.toCharArray()) {
if (Character.isDigit(c)) digits.add(c);
else chars.add(c);
}
if (Math.abs(digits.size() - chars.size()) > 1) return "";
StringBuilder res = new StringBuilder();
boolean flag = digits.size() >= chars.size() ? true : false;
for (int i = 0; i < s.length(); i++){
if (flag) res.append(digits.poll());
else res.append(chars.poll());
flag = !flag;
}
return res.toString();
}
}