Sort an alphanumeric string such that the positions of alphabets and numbers remain unchanged
Last Updated :
22 Dec, 2022
Given an alphanumeric string str, the task is to sort the string in such a way that if a position is occupied by an alphabet it must be occupied by an alphabet after sorting and if occupied by a number it must be occupied by a number after sorting.
Examples:
Input: str = “geeks12for32geeks”
Output: eeeef12ggk23korss
Input: str = “d4c3b2a1”
Output: a1b2c3d4
Approach: We will convert the string to a character array and then sort the character array c[]. After sorting the character array the numeric characters will occupy starting indices of the array and the alphabets will occupy the remaining part of the array.
The numeric half will be sorted and the alphabet part will also be sorted. We will keep two indices one at the starting index of the alphabet part al_c and one at the starting index of numeric part nu_c, now we will check the original string and if a position was occupied by an alphabet then we will replace it with c[al_c] and increment al_c else we will replace it with c[nu_c] and increment nu_c.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string sort(string s)
{
char c[s.length() + 1];
strcpy (c, s.c_str());
sort(c, c + s.length());
int al_c = 0, nu_c = 0;
while (c[al_c] < 97)
al_c++;
for ( int i = 0; i < s.length(); i++) {
if (s[i] < 97)
s[i] = c[nu_c++];
else
s[i] = c[al_c++];
}
return s;
}
int main()
{
string s = "d4c3b2a1" ;
cout << sort(s);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String sort(String s)
{
char []c = new char [s.length() + 1 ];
c = s.toCharArray();
Arrays.sort(c);
int al_c = 0 , nu_c = 0 ;
while (c[al_c] < 97 )
al_c++;
for ( int i = 0 ; i < s.length(); i++)
{
if (s.charAt(i) < 97 )
s = s.substring( 0 ,i)+ c[nu_c++]+s.substring(i+ 1 );
else
s = s.substring( 0 ,i)+ c[al_c++]+s.substring(i+ 1 );
}
return s;
}
public static void main(String[] args)
{
String s = "d4c3b2a1" ;
System.out.println(sort(s));
}
}
|
Python3
def sort(s):
c, s = list (s), list (s)
c.sort()
al_c = 0
nu_c = 0
while ord (c[al_c]) < 97 :
al_c + = 1
for i in range ( len (s)):
if s[i] < 'a' :
s[i] = c[nu_c]
nu_c + = 1
else :
s[i] = c[al_c]
al_c + = 1
return ''.join(s)
if __name__ = = "__main__" :
s = "d4c3b2a1"
print (sort(s))
|
C#
using System;
class GFG
{
static string sort( string s)
{
char []c = new char [s.Length + 1];
c = s.ToCharArray();
Array.Sort(c);
int al_c = 0, nu_c = 0;
while (c[al_c] < 97)
al_c++;
for ( int i = 0; i < s.Length; i++)
{
if (s[i] < 97)
s = s.Substring(0,i)+ c[nu_c++]+s.Substring(i+1);
else
s = s.Substring(0,i)+ c[al_c++]+s.Substring(i+1);
}
return s;
}
public static void Main()
{
string s = "d4c3b2a1" ;
Console.WriteLine(sort(s));
}
}
|
Javascript
<script>
function sort(s)
{
var c = s.split( '' );
c.sort();
var al_c = 0, nu_c = 0;
while (c[al_c].charCodeAt(0) < 97)
al_c++;
for ( var i = 0; i < s.length; i++) {
if (s[i].charCodeAt(0) < 97)
s = s.substring(0,i)+ c[nu_c++]+s.substring(i+1);
else
s = s.substring(0,i)+ c[al_c++]+s.substring(i+1);
}
return s;
}
var s = "d4c3b2a1" ;
document.write( sort(s));
</script>
|
Time complexity: O(N * log(N)) where N is the length of the string.
Auxiliary Space: O(N), where N is the length of the given string.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...