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.
Input: s ="a0b1c2"Output: "0a1b2c"Explanation: No two adjacent characters have the same type in"0a1b2c"."a0b1c2","0a1b2c","0c2a1b" are also valid permutations.
Separate the string into letters and digits. If the difference in their counts is more than 1, it’s impossible. Otherwise, alternate between the two, starting with the type that has more characters.
classSolution {
public: string reformat(string s) {
vector<char> letters, digits;
for (char c : s) {
if (isdigit(c)) digits.push_back(c);
else letters.push_back(c);
}
if (abs((int)letters.size() - (int)digits.size()) >1) return"";
string ans;
bool letterFirst = letters.size() >= digits.size();
int i =0, j =0;
while (i < letters.size() || j < digits.size()) {
if (letterFirst && i < letters.size()) ans += letters[i++];
if (j < digits.size()) ans += digits[j++];
if (!letterFirst && i < letters.size()) ans += letters[i++];
}
return ans;
}
};
classSolution {
funreformat(s: String): String {
val letters = mutableListOf<Char>()
val digits = mutableListOf<Char>()
for (c in s) {
if (c.isDigit()) digits.add(c)
else letters.add(c)
}
if (kotlin.math.abs(letters.size - digits.size) > 1) return""val ans = StringBuilder()
val letterFirst = letters.size >= digits.size
var i = 0; var j = 0while (i < letters.size || j < digits.size) {
if (letterFirst && i < letters.size) ans.append(letters[i++])
if (j < digits.size) ans.append(digits[j++])
if (!letterFirst && i < letters.size) ans.append(letters[i++])
}
return ans.toString()
}
}
classSolution:
defreformat(self, s: str) -> str:
letters = [c for c in s if c.isalpha()]
digits = [c for c in s if c.isdigit()]
if abs(len(letters) - len(digits)) >1:
return"" ans = []
lFirst = len(letters) >= len(digits)
i = j =0while i < len(letters) or j < len(digits):
if lFirst and i < len(letters):
ans.append(letters[i])
i +=1if j < len(digits):
ans.append(digits[j])
j +=1ifnot lFirst and i < len(letters):
ans.append(letters[i])
i +=1return''.join(ans)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
impl Solution {
pubfnreformat(s: String) -> String {
let (mut letters, mut digits): (Vec<char>, Vec<char>) = s.chars().partition(|c| c.is_ascii_alphabetic());
if (letters.len() asi32- digits.len() asi32).abs() >1 { return String::new(); }
letmut ans = String::new();
letmut i =0; letmut j =0;
let letter_first = letters.len() >= digits.len();
while i < letters.len() || j < digits.len() {
if letter_first && i < letters.len() { ans.push(letters[i]); i +=1; }
if j < digits.len() { ans.push(digits[j]); j +=1; }
if!letter_first && i < letters.len() { ans.push(letters[i]); i +=1; }
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
reformat(s: string):string {
constletters: string[] = [], digits: string[] = [];
for (constcofs) {
if (c>='0'&&c<='9') digits.push(c);
elseletters.push(c);
}
if (Math.abs(letters.length-digits.length) >1) return"";
letans='', i=0, j=0;
constletterFirst=letters.length>=digits.length;
while (i<letters.length||j<digits.length) {
if (letterFirst&&i<letters.length) ans+=letters[i++];
if (j<digits.length) ans+=digits[j++];
if (!letterFirst&&i<letters.length) ans+=letters[i++];
}
returnans;
}
}