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
| // @lc code=start
impl Solution {
/// ## 解题思路
pub fn reformat(s: String) -> String {
let char_count = s.chars().filter(|c| c.is_alphabetic()).count() as i32;
let digit_count = (s.len() as i32) - char_count;
if (char_count - digit_count).abs() > 1 {
return "".to_owned();
}
let (mut i, mut j) = match char_count >= digit_count {
true => (0, 1),
false => (1, 0),
};
let mut res_vec = vec![' '; s.len()];
for c in s.chars() {
match c.is_ascii_alphabetic() {
true => {
res_vec[i] = c;
i += 2;
}
false => {
res_vec[j] = c;
j += 2;
}
}
}
res_vec.into_iter().collect()
}
}
// @lc code=end
struct Solution;
|