Sort a String | Practice | GeeksforGeeks

Given a string consisting of lowercase letters, arrange all its letters in ascending order. 
Example 1:
Input:
S = "edcab"
Output: "abcde"
Explanation: characters are in ascending
order in "abcde".

Example 2:
Input:
S = "xzy"
Output: "xyz"
Explanation: characters are in ascending
order in "xyz".



Your Task:  
You don't need to read input or print anything. Your task is to complete the function sort() which takes the string as inputs and returns the modified string.

Expected Time Complexity: O(|S| * log |S|)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ |S| ≤ 105
S contains only lowercase alphabets.
+1
rb0011 week ago
My Java Solution :
 char tempArray[] = inputString.toCharArray();
        // Sorting temp array using
        Arrays.sort(tempArray);
        // Returning new sorted string
        return new String(tempArray);
0
ten000perhr3 weeks ago
C++ One line answer
 
string sort(string s){   //complete the function here   sort(s.begin(), s.end());   return s;}
0
mayank180919994 weeks ago
string sort(string s){
    //complete the function here
    sort(s.begin(),s.end());
    return s;
}
-1
atif836142 months ago
JAVA SOLUTION:
class Solution 
{ 
    String sort(String s) 
    {
        char[] arr=s.toCharArray();
        Arrays.sort(arr);
        return String.valueOf(arr);
    }
} 
-1
priyaranjandash2 months ago
class Solution {    String sort(String s)    {       char ch[] = s.toCharArray();       Arrays.sort(ch);       return String.valueOf(ch);          }} 
+1
imohdalam2 months ago
Java | O(n)
class Solution 
{ 
    String sort(String s) 
    {
        char[] charArray = s.toCharArray();
        
        Arrays.sort(charArray);
        
        StringBuilder ans = new StringBuilder();
        
        for(char x: charArray)
           ans.append(x);
        
        return ans.toString();
    }
}
+1
shubhamkhavare2 months ago
Simple Java Soln:
      char[] c = s.toCharArray();       Arrays.sort(c);       String str = new String();       for(int i = 0 ; i < c.length ; i++)       {           str += c[i];       }       return str;
+1
imsonishivam2 months ago
without any predefine function
Time complexity - O(n)
n - string size;
space complexity - O(1)
 
string sort(string s){   //complete the function here   int count[26] = {0};   for(int i=0;i<s.size();i++){       count[s[i] - 'a']++;   }      s = "\0";      for(int i=0;i<26;i++){       if(count[i] > 0){           while(count[i] != 0){               s.push_back(i+ 'a');               count[i]--;           }       }   }   return s;}
+1
imabheek3 months ago
// JAVA
char [] arr = s.toCharArray();       Arrays.sort(arr);       return new String(arr);
0
anshraj7
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.