Sort the string in descending order in java

Sort the string in descending order in java

Given a string str containing only lower case alphabets, the task is to sort it in lexicographically-descending order.

Input

The line inputs str ,a string.

Constraints:

1 ≤ length of string < 10^5

Output

Print the modified string in new line.

Example

Input:

geeks

Output:

skgee

Explaination

It is the lexicographically-descending order.

Solution of Sort the string in descending order in java:–

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		//your code here
      Scanner sc = new Scanner(System.in);
      String str = sc.next();

      char arr[] = str.toCharArray();

      for(int i=0;i<str.length()-1;i++)
        {
          for(int j=i+1;j<str.length();j++)
            {
              if(arr[i]<arr[j])
              {
                char temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
              }
            }
        }

      String s = new String(arr);
      System.out.print(s);
	}
}

Add a Comment

Your email address will not be published. Required fields are marked *