/** * @param {string}s * @return {string} */ var sortString = function (s) { let res = ""; s = s.split(""); let length = s.length; while (res.length < length) { const map = newSet(); while (s.length) { let min = "{"; let minIndex = -1; for (let i = 0; i < s.length; i++) { if (s[i] < min && !map.has(s[i])) { min = s[i]; minIndex = i; } } if (min === "{") break; s.splice(minIndex, 1); map.add(min); res += min; } map.clear(); while (s.length) { let max = "`"; let maxIndex = -1; for (let i = 0; i < s.length; i++) { if (s[i] > max && !map.has(s[i])) { max = s[i]; maxIndex = i; } } if (max === "`") break; s.splice(maxIndex, 1); map.add(max); res += max; } } return res; };
/** * @param {string}s * @return {string} */ var sortString = function (s) { let res = ""; const arr = newArray(26).fill(0); for (let i = 0; i < s.length; i++) arr[s.charCodeAt(i) - 97]++; while (res.length < s.length) { for (let i = 0; i < 26; i++) { if (arr[i]) { res += String.fromCharCode(i + 97);
arr[i]--; } } for (let i = 25; i >= 0; i--) { if (arr[i]) { res += String.fromCharCode(i + 97); arr[i]--; } } } return res; };