Reformat The String
给一个string, 问能不能重组这个string, 让数字和字母间隔分布.
class Solution {
public String reformat(String s) {
if(s == null || s.isEmpty()) return s;
String characters = "";
String digits = "";
for(char c : s.toCharArray()){
if(Character.isDigit(c)) digits += c;
if(Character.isLetter(c)) characters += c;
}
if(Math.abs(digits.length() - characters.length()) > 1) return "";
if(digits.length() > characters.length()) return getString(characters,digits);
return getString(digits,characters);
}
private String getString(String first, String second){
int i = 0;
String result = "";
while(i < first.length()) result += second.charAt(i) + "" + first.charAt(i++);
if(i < second.length()) result += second.charAt(i);
return result;
}
}