Sort a string in increasing order of given priorities
Last Updated :
10 Jun, 2021
Given an alphanumeric string S of length N, the task is to sort the string in increasing order of their priority based on the following conditions:
- Characters with even ASCII values have higher priority than characters with odd ASCII values.
- Even digits have higher priority than odd digits.
- Digits have higher priority than characters.
- For characters or digits having the same parity, the priority is in increasing order of their ASCII values.
Examples:
Input: S = “abcd1234”
Output: 1324bdac
Explanation:
The ASCII value of “a” is 97.
The ASCII value of “b” is 98.
The ASCII value of “c” is 99.
The ASCII value of “d” is 100.
Since characters with even ASCII value have higher priority, “b” and “d” are placed first followed by “a” and “c”.
Similarly, even digits have more priority than odd digits. Therefore, they are placed first.
Since the numbers have a higher priority than characters, the sorted string is “1324bdac”
Input: S = “adb123”
Output: bda132
Approach: The idea is to separate the characters with odd and even ASCII values and also the digits with odd and even parity. Then, join these substrings in the order of their priorities. Follow the steps below to solve the problem:
- Initialize two variables, say digits and characters, to store the characters and digits separately.
- Sort the strings digits and characters with respect to the ASCII table.
- Now, traverse all the characters in characters and append each character with an odd parity to a variable say oddChars and each character with an even parity to another variable say, evenChars.
- Similarly, for the string digits, separate the odd and even parity digits, say oddDigs and evenDigs.
- Finally, concatenate the string as oddChars + evenChars + oddDigs + evenDigs.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
string sortString(string s)
{
string digits = "" ;
string character = "" ;
for ( int i = 0; i < s.size(); ++i) {
if (s[i] >= '0' && s[i] <= '9' )
{
digits += s[i];
}
else
{
character += s[i];
}
}
sort(digits.begin(), digits.end());
sort(character.begin(), character.end());
string OddChar = "" , EvenChar = "" ;
for ( int i = 0; i < digits.length(); ++i) {
if ((digits[i] - '0' ) % 2 == 1) {
OddChar += digits[i];
}
else {
EvenChar += digits[i];
}
}
OddChar += EvenChar;
EvenChar = "" ;
for ( int i = 0; i < character.length();
++i) {
if ((character[i] - 'a' ) % 2 == 1) {
OddChar += character[i];
}
else {
EvenChar += character[i];
}
}
OddChar += EvenChar;
return OddChar;
}
int main()
{
string s = "abcd1234" ;
cout << sortString(s);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String sortString(String s)
{
String digits = "" ;
String character = "" ;
for ( int i = 0 ; i < s.length(); ++i) {
if (s.charAt(i) >= '0' && s.charAt(i) <= '9' ) {
digits += s.charAt(i);
} else {
character += s.charAt(i);
}
}
digits = sort(digits);
character = sort(character);
String OddChar = "" , EvenChar = "" ;
for ( int i = 0 ; i < digits.length(); ++i) {
if ((digits.charAt(i) - '0' ) % 2 == 1 ) {
OddChar += digits.charAt(i);
} else {
EvenChar += digits.charAt(i);
}
}
OddChar += EvenChar;
EvenChar = "" ;
for ( int i = 0 ; i < character.length(); ++i) {
if ((character.charAt(i) - 'a' ) % 2 == 1 ) {
OddChar += character.charAt(i);
} else {
EvenChar += character.charAt(i);
}
}
OddChar += EvenChar;
return OddChar;
}
static String sort(String inputString)
{
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
public static void main(String[] args)
{
String s = "abcd1234" ;
System.out.print(sortString(s));
}
}
|
Python3
def sortString(s):
digits = ""
character = ""
for i in range ( len (s)):
if (s[i] > = '0' and s[i] < = '9' ):
digits + = s[i]
else :
character + = s[i]
Digits = list (digits)
Digits.sort()
Character = list (character)
Character.sort()
OddChar, EvenChar = " ", " "
for i in range ( len (Digits)):
if (( ord (digits[i]) - ord ( '0' )) % 2 = = 1 ):
OddChar + = Digits[i]
else :
EvenChar + = Digits[i]
OddChar + = EvenChar
EvenChar = ""
for i in range ( len (Character)):
if (( ord (Character[i]) - ord ( 'a' )) % 2 = = 1 ):
OddChar + = Character[i]
else :
EvenChar + = Character[i]
OddChar + = EvenChar
return OddChar
s = "abcd1234"
print (sortString( list (s)))
|
C#
using System;
class GFG
{
static string sortString( char [] s)
{
string digits = "" ;
string character = "" ;
for ( int i = 0; i < s.Length; ++i)
{
if (s[i] >= '0' && s[i] <= '9' )
{
digits += s[i];
}
else
{
character += s[i];
}
}
char [] Digits = digits.ToCharArray();
Array.Sort(Digits);
char [] Character = character.ToCharArray();
Array.Sort(Character);
string OddChar = "" , EvenChar = "" ;
for ( int i = 0; i < Digits.Length; ++i)
{
if ((digits[i] - '0' ) % 2 == 1)
{
OddChar += Digits[i];
}
else
{
EvenChar += Digits[i];
}
}
OddChar += EvenChar;
EvenChar = "" ;
for ( int i = 0; i < Character.Length; ++i)
{
if ((Character[i] - 'a' ) % 2 == 1)
{
OddChar += Character[i];
}
else
{
EvenChar += Character[i];
}
}
OddChar += EvenChar;
return OddChar;
}
static void Main()
{
string s = "abcd1234" ;
Console.WriteLine(sortString(s.ToCharArray()));
}
}
|
Javascript
<script>
function sortString(s)
{
let digits = "" ;
let character = "" ;
for (let i = 0; i < s.length; ++i)
{
if (s[i].charCodeAt() >= '0' .charCodeAt() && s[i].charCodeAt() <= '9' .charCodeAt())
{
digits += s[i];
}
else
{
character += s[i];
}
}
let Digits = digits.split( '' );
Digits.sort();
let Character = character.split( '' );
Character.sort();
let OddChar = "" , EvenChar = "" ;
for (let i = 0; i < Digits.length; ++i)
{
if ((digits[i].charCodeAt() - '0' .charCodeAt()) % 2 == 1)
{
OddChar += Digits[i];
}
else
{
EvenChar += Digits[i];
}
}
OddChar += EvenChar;
EvenChar = "" ;
for (let i = 0; i < Character.length; ++i)
{
if ((Character[i].charCodeAt() - 'a' .charCodeAt()) % 2 == 1)
{
OddChar += Character[i];
}
else
{
EvenChar += Character[i];
}
}
OddChar += EvenChar;
return OddChar;
}
let s = "abcd1234" ;
document.write(sortString(s.split( '' )));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...