-
Notifications
You must be signed in to change notification settings - Fork 0
/
Counting Sort.cpp
51 lines (43 loc) · 908 Bytes
/
Counting Sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//{ Driver Code Starts
//Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
#define RANGE 255
// } Driver Code Ends
//User function Template for C++
class Solution{
public:
//Function to arrange all letters of a string in lexicographical
//order using Counting Sort.
string countSort(string arr){
// code here
vector<int>freq(26,0);
for(int i=0;i<arr.length();i++){
freq[arr[i]-'a']++;
}
string ans="";
for(int i=0;i<26;i++){
for(int j=0;j<freq[i];j++){
ans+=i+'a';
}
}
return ans;
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
string arr;
cin>>arr;
Solution obj;
cout<<obj.countSort(arr)<<endl;
}
return 0;
}
// } Driver Code Ends