문제 링크 : Increasing Decreasing String
/**
* @param {string} s
* @return {string}
*/
var sortString = function(s) {
const sObj = {}
s = s.split('').sort()
for(let el of s) {
sObj[el] ? sObj[el] += 1 : sObj[el] = 1
}
let chk = true
let result = ''
while(chk) {
let str = result
for(let key in sObj) {
if(sObj[key] > 0) {
str += key
sObj[key] -= 1
}
}
let reverseStr = ''
for(let key in sObj) {
if(sObj[key] > 0) {
reverseStr += key
sObj[key] -= 1
}
}
str += reverseStr.split('').reverse().join('')
str === result ? chk = false : result = str
}
return result
};
/**
* @param {string} s
* @return {string}
*/
var sortString = function(s) {
let temp = new Array(26).fill(0);
let ans = [];
for (let i=0; i<s.length; i++) {
temp[s.charCodeAt(i) - 97] += 1;
}
let flag = true;
while(ans.length < s.length) {
for (let i=0; i<temp.length; i++) {
var pos = i;
if (!flag) {
pos = 25-i;
}
if (temp[pos] !==0) {
ans.push(String.fromCharCode(pos+97));
temp[pos] -= 1;
}
}
flag = !flag;
}
ans = ans.join('');
return ans;
};