class Solution:
def reformat(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 ""
res = []
# Start with the longer group if uneven
if len(letters) > len(digits):
first, second = letters, digits
else:
first, second = digits, letters
for i in range(len(s)):
if i % 2 == 0:
if first:
res.append(first.pop())
else:
if second:
res.append(second.pop())
return ''.join(res)
class Solution {
public:
string reformat(string s) {
vector<char> letters, digits;
for (char c : s) {
if (isalpha(c)) letters.push_back(c);
else if (isdigit(c)) digits.push_back(c);
}
if (abs((int)letters.size() - (int)digits.size()) > 1) return "";
string res = "";
bool letterFirst = letters.size() >= digits.size();
int i = 0, j = 0;
for (int k = 0; k < s.length(); ++k) {
if ((k % 2 == 0) == letterFirst) {
res += letters[i++];
} else {
res += digits[j++];
}
}
return res;
}
};
class Solution {
public String reformat(String s) {
List<Character> letters = new ArrayList<>();
List<Character> digits = new ArrayList<>();
for (char c : s.toCharArray()) {
if (Character.isLetter(c)) letters.add(c);
else if (Character.isDigit(c)) digits.add(c);
}
if (Math.abs(letters.size() - digits.size()) > 1) return "";
StringBuilder sb = new StringBuilder();
boolean letterFirst = letters.size() >= digits.size();
int i = 0, j = 0;
for (int k = 0; k < s.length(); ++k) {
if ((k % 2 == 0) == letterFirst) {
sb.append(letters.get(i++));
} else {
sb.append(digits.get(j++));
}
}
return sb.toString();
}
}
var reformat = function(s) {
let letters = [], digits = [];
for (let c of s) {
if (/[a-zA-Z]/.test(c)) letters.push(c);
else if (/[0-9]/.test(c)) digits.push(c);
}
if (Math.abs(letters.length - digits.length) > 1) return "";
let res = [];
let letterFirst = letters.length >= digits.length;
let i = 0, j = 0;
for (let k = 0; k < s.length; ++k) {
if ((k % 2 === 0) === letterFirst) {
res.push(letters[i++]);
} else {
res.push(digits[j++]);
}
}
return res.join('');
};
Given a string s
consisting of lowercase English letters and digits, reformat it so that no two adjacent characters are of the same type (i.e., no two letters or two digits are next to each other). Return any
possible reformatted string that meets this condition, or return an empty string if it is impossible.
s
must be used exactly once.The problem asks us to rearrange the string so that no two adjacent characters are both letters or both digits. A brute-force approach would be to try all possible permutations, but that is highly inefficient and unnecessary here. Instead, we can use the observation that the only way to alternate perfectly is if the counts of letters and digits are nearly equal (differ by at most 1).
Therefore, our first step is to count the number of letters and digits. If their counts differ by more than 1, it's impossible. Otherwise, we can alternate between the two types, starting with the type that has more characters. This way, we ensure no two adjacent characters are of the same type.
The problem becomes one of partitioning the string into two lists (letters and digits) and then merging them in alternating order.
s
and place letters in one list and digits in another.
This approach is efficient and direct, avoiding unnecessary computation.
Consider the input s = "a0b1c2"
.
"a0b1c2"
"0a1b2c"
is also valid.Notice how no two adjacent characters are of the same type, and all original characters are used exactly once.
s
. We make a single pass to separate characters and one more to build the result.The key insight is that alternating letters and digits is only possible when their counts differ by at most one. By splitting the input into two lists and merging them in alternating order, we efficiently build a valid reformatted string or determine impossibility. This approach is both simple and optimal for the problem constraints.