-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path010.Counting_sort_alphabets.cpp
55 lines (46 loc) · 1.16 KB
/
010.Counting_sort_alphabets.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
52
53
54
55
//counting sort using alphabets
//https://www.geeksforgeeks.org/problems/counting-sort/1?itm_source=geeksforgeeks&itm_medium=article&itm_campaign=practice_card
//Given a string arr consisting of lowercase english letters, arrange all its letters in lexicographical order using Counting Sort.
//{ 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:
string countSort(string arr){
string h = "";
for (int i = 0; i < arr.size(); i++) h += 'a';
vector <int> a(26);
for (auto i : arr) {
a[i - 'a']++;
}
for (int i = 0; i < 26; i++) {
a[i] += a[i - 1];
}
for (int i = arr.size() - 1; i >= 0; i--) {
h[--a[arr[i] - 'a']] = arr[i];
}
return h;
}
};
//{ 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;
cout << "~" << "\n";
}
return 0;
}
// } Driver Code Ends