Sort groups of numbers and characters separately in a String
Given string str of alphanumeric characters, the task is to sort the similar group of consecutive characters separately and print the modified string i.e. all consecutive groups of digits and alphabetical characters will be sorted separately.
Examples:
Input: str = “121geeks21”
Output: 112eegks12
Explanation: “121”, “geeks”, “21” are the valid groups
and they will be sorted separately.
Input: str = “cba321ab”
Output: abc123ab
Approach: Create a vector to store the starting indices of all the valid groups in the given string. Now, traverse the string character by character and if the current character is from a different group than the previous character then push the current index to the vector. After the string has been traversed completely, sort all the individual groups of a substring of the given string using the vector updated earlier. Finally, print the modified string.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
string get_string(string str, int n)
{
char prev = str[0];
vector< int > result;
result.push_back(0);
for ( int i = 1; i < n; i++) {
if ( isdigit (str[i]) != isdigit (prev)) {
result.push_back(i);
}
prev = str[i];
}
sort(str.begin(), str.begin() + result[0]);
for ( int i = 0; i < result.size() - 1; i++) {
sort(str.begin() + result[i],
str.begin() + result[i + 1]);
}
sort(str.begin() + result[result.size() - 1],
str.end());
return str;
}
int main()
{
string str = "121geeks21" ;
int n = str.length();
cout << get_string(str, n);
return 0;
}
|
Python3
def get_string(st, n):
prev = st[ 0 ]
result = []
result.append( 0 )
for i in range ( 1 , n):
if (st[i].isdigit() ! = prev.isdigit()):
result.append(i)
prev = st[i]
st = list (st)
st[:result[ 0 ]].sort()
p = st.copy()
for i in range ( len (result) - 1 ):
p[result[i]:result[i + 1 ]] = sorted (st[result[i]:result[i + 1 ]])
p[ len (p) - 2 :] = sorted (st[result[ - 1 ]:])
return ''.join(p)
if __name__ = = "__main__" :
st = "121geeks21"
n = len (st)
print (get_string(st, n))
|
Java
import java.util.*;
public class Main {
static String get_string(String str, int n) {
char prev = str.charAt( 0 );
List<Integer> result = new ArrayList<>();
result.add( 0 );
for ( int i = 1 ; i < n; i++) {
if (Character.isDigit(str.charAt(i)) != Character.isDigit(prev)) {
result.add(i);
}
prev = str.charAt(i);
}
char [] charArray = str.toCharArray();
Arrays.sort(charArray, 0 , result.get( 0 ));
for ( int i = 0 ; i < result.size() - 1 ; i++) {
Arrays.sort(charArray, result.get(i), result.get(i + 1 ));
}
Arrays.sort(charArray, result.get(result.size() - 1 ), n);
return new String(charArray);
}
public static void main(String[] args) {
String str = "121geeks21" ;
int n = str.length();
System.out.println(get_string(str, n));
}
}
|
C#
using System;
using System.Collections.Generic;
public class Program {
static string GetModifiedString( string str, int n) {
char prev = str[0];
List< int > result = new List< int >();
result.Add(0);
for ( int i = 1; i < n; i++) {
if ( char .IsDigit(str[i]) != char .IsDigit(prev)) {
result.Add(i);
}
prev = str[i];
}
char [] charArray = str.ToCharArray();
Array.Sort(charArray, 0, result[0]);
for ( int i = 0; i < result.Count - 1; i++) {
Array.Sort(charArray, result[i], result[i + 1] - result[i]);
}
Array.Sort(charArray, result[result.Count - 1], n - result[result.Count - 1]);
return new string (charArray);
}
public static void Main( string [] args) {
string str = "121geeks21" ;
int n = str.Length;
Console.WriteLine(GetModifiedString(str, n));
}
}
|
Javascript
function get_string(str, n)
{
let prev = str[0];
let result = [];
result.push(0);
for (let i = 1; i < n; i++) {
if (isNaN(parseInt(str[i])) !== isNaN(parseInt(prev))) {
result.push(i);
}
prev = str[i];
}
str = str.substr(0, result[0]) +
str.substr(result[0], result[1] - result[0])
.split( '' ).sort().join( '' ) +
str.substr(result[1]);
for (let i = 1; i < result.length - 1; i++) {
str = str.substr(0, result[i]) +
str.substr(result[i], result[i + 1] - result[i])
.split( '' ).sort().join( '' ) +
str.substr(result[i + 1]);
}
str = str.substr(0, result[result.length - 1]) +
str.substr(result[result.length - 1])
.split( '' ).sort().join( '' );
return str;
}
let str = "121geeks21" ;
let n = str.length;
console.log(get_string(str, n));
|
Time complexity: O(n*nlogn)
Auxiliary Space: O(n)
Last Updated :
23 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...