Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- A string S is given consisting of lowercase alphabetical characters only. You need to return a sorted string using Count Sort.
- Input format
- First line will contain a single integer n representing size of the given string.
- Second line will contain a single string S of size n.
- Output format
- Output the string in a single line.
- Sample Input 1
- 10
- abcdeedcba
- Sample Output 1
- aabbccddee
- Constraints
- 1<=n<=10^6
- String S will contain lowercase alphabetical characters only
- */
- /**
- * @param {number} n
- * @param {string} s
- * @return {string}
- */
- function countSort(n, str) {
- let frequencyCount = new Array(26).fill(0);
- for (let i = 0; i < n; i++) {
- const charCode = str.charCodeAt(i) - "a".charCodeAt(0);
- frequencyCount[charCode]++;
- }
- let sortedString = "";
- for (let i = 0; i < 26; i++) {
- for (let j = 0; j < frequencyCount[i]; j++) {
- sortedString = sortedString + String.fromCharCode("a".charCodeAt(0) + i);
- }
- }
- return sortedString;
- }
- function main() {
- let n = parseInt(readLine());
- let str = readLine();
- let storedString = countSort(n, str);
- print(storedString)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement