class Solution {
public:
string reformat(string s) {
int digitCount = 0;
int letterCount = 0;
for (auto ch : s) {
if (isdigit(ch)) {
digitCount++;
}
else if(isalpha(ch)) {
letterCount++;
}
}
if (abs(digitCount - letterCount) > 1) {
return "";
}
int i = digitCount > letterCount ? 0 : 1;
int j = digitCount > letterCount ? 1 : 0;
vector<char> res(s.size());
for (auto ch : s) {
if (isdigit(ch)) {
res[i] = ch;
i += 2;
}
else if (isalpha(ch)) {
res[j] = ch;
j += 2;
}
}
return string(res.begin(), res.end());
}
};
【数组】1417. 重新格式化字符串
最新推荐文章于 2024-03-31 02:31:01 发布