Program to sort string in descending order
Last Updated :
01 Nov, 2022
Given a string, sort it in descending order.
Examples:
Input : alkasingh
Output : snlkihgaa
Input : nupursingh
Output : uusrpnnihg
Input : geeksforgeeks
Output : ssrokkggfeeee
A simple solution is to use library sort function std::sort()
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void descOrder(string &s)
{
sort(s.begin(), s.end(), greater< char >());
}
int main()
{
string s = "geeksforgeeks" ;
descOrder(s);
for ( int i = 0; i < s.size(); i++)
cout << s[i];
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void descOrder( char [] s)
{
Arrays.sort(s);
reverse(s);
}
static void reverse( char [] a)
{
int i, n = a.length;
char t;
for (i = 0 ; i < n / 2 ; i++)
{
t = a[i];
a[i] = a[n - i - 1 ];
a[n - i - 1 ] = t;
}
}
public static void main(String[] args)
{
char [] s = "geeksforgeeks" .toCharArray();
descOrder(s);
System.out.println(String.valueOf(s));
}
}
|
Python
def descOrder(s):
s.sort(reverse = True )
str1 = ''.join(s)
print (str1)
def main():
s = list ( 'geeksforgeeks' )
descOrder(s)
if __name__ = = "__main__" :
main()
|
C#
using System;
class GFG
{
static void descOrder( char [] s)
{
Array.Sort(s);
reverse(s);
}
static void reverse( char [] a)
{
int i, n = a.Length;
char t;
for (i = 0; i < n / 2; i++)
{
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
public static void Main(String[] args)
{
char [] s = "geeksforgeeks" .ToCharArray();
descOrder(s);
Console.WriteLine(String.Join( "" ,s));
}
}
|
PHP
<?php
function descOrder( $s )
{
$s = str_split ( $s );
rsort( $s );
echo implode( '' , $s );
}
$s = "geeksforgeeks" ;
descOrder( $s );
?>
|
Javascript
<script>
function descOrder(s) {
s.sort().reverse();
str1 = s.join( "" );
document.write(str1);
}
var s = "geeksforgeeks" ;
s = s.split( "" );
descOrder(s);
</script>
|
The time complexity is : O(n log n)
Auxiliary Space: O(1)
An efficient approach will be to observe first that there can be a total of 26 unique characters only. So, we can store the count of occurrences of all the characters from ‘a’ to ‘z’ in a hashed array. The first index of the hashed array will represent character ‘a’, second will represent ‘b’ and so on. Finally, we will simply traverse the hashed array and print the characters from ‘z’ to ‘a’ the number of times they occurred in input string.
Below is the implementation of above idea:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
void sortString(string& str)
{
int charCount[MAX_CHAR] = { 0 };
for ( int i = 0; i < str.length(); i++)
charCount[str[i] - 'a' ]++;
for ( int i = MAX_CHAR - 1; i >= 0; i--)
for ( int j = 0; j < charCount[i]; j++)
cout << ( char )( 'a' + i);
}
int main()
{
string s = "alkasingh" ;
sortString(s);
return 0;
}
|
Java
class GFG
{
static int MAX_CHAR = 26 ;
static void sortString(String str)
{
int charCount[] = new int [MAX_CHAR];
for ( int i = 0 ; i < str.length(); i++)
{
charCount[str.charAt(i) - 'a' ]++;
}
for ( int i = MAX_CHAR - 1 ; i >= 0 ; i--)
{
for ( int j = 0 ; j < charCount[i]; j++)
{
System.out.print(( char ) ( 'a' + i));
}
}
}
public static void main(String[] args)
{
String s = "alkasingh" ;
sortString(s);
}
}
|
Python3
MAX_CHAR = 26 ;
def sortString( str ):
charCount = [ 0 ] * MAX_CHAR;
for i in range ( len ( str )):
charCount[ ord ( str [i]) - ord ( 'a' )] + = 1 ;
for i in range (MAX_CHAR - 1 , - 1 , - 1 ):
for j in range (charCount[i]):
print ( chr ( 97 + i),end = "");
s = "alkasingh" ;
sortString(s);
|
C#
using System;
class GFG
{
static int MAX_CHAR = 26;
static void sortString(String str)
{
int []charCount = new int [MAX_CHAR];
for ( int i = 0; i < str.Length; i++)
{
charCount[str[i] - 'a' ]++;
}
for ( int i = MAX_CHAR - 1; i >= 0; i--)
{
for ( int j = 0; j < charCount[i]; j++)
{
Console.Write(( char ) ( 'a' + i));
}
}
}
public static void Main(String[] args)
{
String s = "alkasingh" ;
sortString(s);
}
}
|
Javascript
<script>
let MAX_CHAR = 26;
function sortString(str)
{
let charCount = new Array(MAX_CHAR);
for (let i = 0; i < charCount.length; i++)
{
charCount[i] = 0;
}
for (let i = 0; i < str.length; i++)
{
charCount[str[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
for (let i = MAX_CHAR - 1; i >= 0; i--)
{
for (let j = 0; j < charCount[i]; j++)
{
document.write(String.fromCharCode ( 'a' .charCodeAt(0) + i));
}
}
}
let s = "alkasingh" ;
sortString(s);
</script>
|
Time Complexity: O( n ), where n is the length of input string.
Auxiliary Space: O( 1 ).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...