![]() |
Given a string S of size N, the task is to sort the string based on its ASCII values. Examples:
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:
Below is the implementation of the above approach: C++
Java
Python3
C#
Javascript
Output
FGGeeeekkorss Time Complexity: O(256*N) 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++
Java
Python3
C#
Javascript
Output
FGGeeeekkorss Time Complexity: O(N*log N) |
Reffered: https://www.geeksforgeeks.org
Strings |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |