forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
34 lines (34 loc) · 962 Bytes
/
Solution.java
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
class Solution {
public String reformat(String s) {
StringBuilder a = new StringBuilder();
StringBuilder b = new StringBuilder();
for (char c : s.toCharArray()) {
if (Character.isDigit(c)) {
a.append(c);
} else {
b.append(c);
}
}
int m = a.length(), n = b.length();
if (Math.abs(m - n) > 1) {
return "";
}
StringBuilder ans = new StringBuilder();
for (int i = 0; i < Math.min(m, n); ++i) {
if (m > n) {
ans.append(a.charAt(i));
ans.append(b.charAt(i));
} else {
ans.append(b.charAt(i));
ans.append(a.charAt(i));
}
}
if (m > n) {
ans.append(a.charAt(m - 1));
}
if (m < n) {
ans.append(b.charAt(n - 1));
}
return ans.toString();
}
}