Sort the string as per ASCII values of the characters
Last Updated :
08 Feb, 2024
Given a string S of size N, the task is to sort the string based on its ASCII values.
Examples:
Input: S = “Geeks7”
Output: 7Geeks
Explanation: According to the ASCII values, integers comes first, then capital alphabets and the small alphabets.
Input: S = “GeeksForGeeks”
Output: FGGeeeekkorss
Approach: The idea to solve this problem is to maintain an array to store the frequency of each character and then add them accordingly in the resultant string. Since there are at max of 256 characters it mmakeske the space complexity constant. Follow the steps below to solve this problem:
- Initialize a vector freq[] of size 256 with values 0 to store the frequency of each character of the string.
- Iterate over the range [0, N) using the variable i and increase the count of s[i] in the array freq[] by 1.
- Make the string S as an empty string.
- Iterate over the range [0, N) using the variable i and iterate over the range [0, freq[i]) using the variable j and adding the character corresponding to the i-th ASCII value in the string s[].
- After performing the above steps, print the string S as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sortString(string s)
{
int N = s.length();
vector< int > freq(256, 0);
for ( int i = 0; i < N; i++) {
freq[s[i]]++;
}
s = "" ;
for ( int i = 0; i < 256; i++) {
for ( int j = 0; j < freq[i]; j++)
s = s + ( char )i;
}
cout << s << "\n" ;
return ;
}
int main()
{
string S = "GeeksForGeeks" ;
sortString(S);
return 0;
}
|
Java
import java.util.*;
class Main
{
static void sortString(String s)
{
int N = s.length();
int [] freq = new int [ 256 ];
for ( int i = 0 ; i < 256 ; i++){
freq[i] = 0 ;
}
for ( int i = 0 ; i < N; i++) {
char character = s.charAt(i);
int val = ( int ) character;
freq[val]++;
}
for ( int i = 0 ; i < 256 ; i++) {
for ( int j = 0 ; j < freq[i]; j++)
System.out.print(( char )i);
}
}
public static void main(String [] args)
{
String S = "GeeksForGeeks" ;
sortString(S);
}
}
|
Python3
def sortString(s):
N = len (s)
freq = [ 0 ] * 256
for i in range ( 0 , N) :
freq[ ord (s[i])] + = 1
s = ""
for i in range ( 256 ):
for j in range (freq[i]):
s = s + chr (i)
print (s)
return
S = "GeeksForGeeks"
sortString(S)
|
C#
using System;
class GFG
{
static void sortString( string s)
{
int N = s.Length;
int [] freq = new int [256];
for ( int i = 0; i < 256; i++){
freq[i] = 0;
}
for ( int i = 0; i < N; i++) {
char character = s[i];
int val = ( int ) character;
freq[val]++;
}
for ( int i = 0; i < 256; i++) {
for ( int j = 0; j < freq[i]; j++)
Console.Write(( char )i);
}
}
public static void Main()
{
string S = "GeeksForGeeks" ;
sortString(S);
}
}
|
Javascript
<script>
function sortString(s)
{
let N = s.length;
let freq = new Array(256).fill(0)
for (let i = 0; i < N; i++) {
freq[s[i].charCodeAt(0)]++;
}
s = "" ;
for (let i = 0; i < 256; i++) {
for (let j = 0; j < freq[i]; j++)
s = s + String.fromCharCode(i);
}
document.write(s);
return ;
}
let S = "GeeksForGeeks" ;
sortString(S);
</script>
|
Time Complexity: O(256*N)
Auxiliary Space: O(256)
Alternate Approach: The given problem can also be solved by using the comparator function with the inbuilt sort() function to sort the given string as per their ASCII values.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
bool cmp( char ch, char chh) { return int (ch) <= int (chh); }
string sortString(string S)
{
sort(S.begin(), S.end(), cmp);
return S;
}
int main()
{
string S = "GeeksForGeeks" ;
cout << sortString(S);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static String sortString(String S)
{
char [] tempArray = S.toCharArray();
Arrays.sort(tempArray);
return String.valueOf(tempArray);
}
public static void main(String[] args)
{
String S = "GeeksForGeeks" ;
System.out.print(sortString(S));
}
}
|
Python3
S = "GeeksForGeeks"
string = ''.join( sorted (S))
print (string)
|
C#
using System;
public class GFG{
static String sortString(String S)
{
char [] tempArray = S.ToCharArray();
Array.Sort(tempArray);
return new string (tempArray);
}
static public void Main (){
string S = "GeeksForGeeks" ;
Console.Write(sortString(S));
}
}
|
Javascript
function sortString(S)
{
arr = S.split( "" );
arr.sort();
return arr.join( "" );
}
var S = "GeeksForGeeks" ;
console.log(sortString(S));
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...