Alternate Lower Upper String Sort
Last Updated :
22 Jun, 2022
Given a string containing lowercase and uppercase letters. Sort it in such a manner that the uppercase and lowercase letters come in an alternate manner but in a sorted way.
Examples:
Input : bAwutndekWEdkd
Output :AbEdWddekkntuw
Explanation:
Here we can see that letter ‘A’, ’E’, ’W’ are sorted
as well as letters “b, d, d, d, e, k, k, n, t, u, w” are sorted
but both appears alternately in the string as far as possible.
Input :abbfDDhGFBvdFDGBNDasZVDFjkb
Output :BaBaDbDbDbDdDfFhFjFkGsGvNVZ
1) Count lower case characters in an array lCount[]
2) Count upper case characters in another array uCount[]
3) Modify strings using lCount[] and uCount[]
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 26
void alternateSort(string& s)
{
int n = s.length();
int lCount[MAX] = { 0 }, uCount[MAX] = { 0 };
for ( int i = 0; i < n; i++) {
if ( isupper (s[i]))
uCount[s[i] - 'A' ]++;
else
lCount[s[i] - 'a' ]++;
}
int i = 0, j = 0, k = 0;
while (k < n) {
while (i < MAX && uCount[i] == 0)
i++;
if (i < MAX) {
s[k++] = 'A' + i;
uCount[i]--;
}
while (j < MAX && lCount[j] == 0)
j++;
if (j < MAX) {
s[k++] = 'a' + j;
lCount[j]--;
}
}
}
int main()
{
string str = "bAwutndekWEdkd" ;
alternateSort(str);
cout << str << "\n" ;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG{
private final static int MAX = 100 ;
public static String alternateSort(String s1)
{
int n = s1.length();
char [] s = s1.toCharArray();
int [] lCount = new int [MAX];
int [] uCount = new int [MAX];
for ( int i = 0 ; i < n; i++) {
if (Character.isUpperCase(s[i]))
uCount[s[i] - 'A' ]++;
else
lCount[s[i] - 'a' ]++;
}
int i = 0 , j = 0 , k = 0 ;
while (k < n)
{
while (i < MAX && uCount[i] == 0 )
i++;
if (i < MAX) {
s[k++] = ( char )( 'A' + i);
uCount[i]--;
}
while (j < MAX && lCount[j] == 0 )
j++;
if (j < MAX) {
s[k++] = ( char )( 'a' + j);
lCount[j]--;
}
}
return ( new String(s));
}
public static void main(String argc[]){
String str = "bAwutndekWEdkd" ;
System.out.println(alternateSort(str));
}
}
|
C#
using System;
public class GFG {
private static int MAX = 100;
static String alternateSort(String s1)
{
int n = s1.Length;
int l = 0, j = 0, k = 0;
char [] s = s1.ToCharArray();
int [] lCount = new int [MAX];
int [] uCount = new int [MAX];
for ( int i = 0; i < n; i++) {
if ( char .IsUpper(s[i]))
uCount[s[i] - 'A' ]++;
else
lCount[s[i] - 'a' ]++;
}
while (k < n)
{
while (l < MAX && uCount[l] == 0)
l++;
if (l < MAX) {
s[k++] = ( char )( 'A' + l);
uCount[l]--;
}
while (j < MAX && lCount[j] == 0)
j++;
if (j < MAX) {
s[k++] = ( char )( 'a' + j);
lCount[j]--;
}
}
return ( new String(s));
}
public static void Main()
{
String str = "bAwutndekWEdkd" ;
Console.Write(alternateSort(str));
}
}
|
Python3
MAX = 26
def alternateSort(s):
n = len (s)
lCount = [ 0 for i in range ( MAX )]
uCount = [ 0 for i in range ( MAX )]
s = list (s)
for i in range (n):
if (s[i].isupper()):
uCount[ ord (s[i]) -
ord ( 'A' )] + = 1
else :
lCount[ ord (s[i]) -
ord ( 'a' )] + = 1
i = 0
j = 0
k = 0
while (k < n):
while (i < MAX and
uCount[i] = = 0 ):
i + = 1
if (i < MAX ):
s[k] = chr ( ord ( 'A' ) + i)
k + = 1
uCount[i] - = 1
while (j < MAX and
lCount[j] = = 0 ):
j + = 1
if (j < MAX ):
s[k] = chr ( ord ( 'a' ) + j)
k + = 1
lCount[j] - = 1
print ("".join(s))
str = "bAwutndekWEdkd"
alternateSort( str )
|
Javascript
<script>
const MAX = 26;
function alternateSort(s1) {
var n = s1.length;
var l = 0, j = 0, k = 0;
var s = s1.split( "" );
var lCount = new Array(MAX).fill(0);
var uCount = new Array(MAX).fill(0);
for ( var i = 0; i < n; i++) {
if (s[i] === s[i].toUpperCase())
uCount[s[i].charCodeAt(0) - "A" .charCodeAt(0)]++;
else
lCount[s[i].charCodeAt(0) - "a" .charCodeAt(0)]++;
}
while (k < n) {
while (l < MAX && uCount[l] === 0)
l++;
if (l < MAX) {
s[k++] = String.fromCharCode( "A" .charCodeAt(0) + l);
uCount[l]--;
}
while (j < MAX && lCount[j] === 0)
j++;
if (j < MAX) {
s[k++] = String.fromCharCode( "a" .charCodeAt(0) + j);
lCount[j]--;
}
}
return s.join( "" );
}
var str = "bAwutndekWEdkd" ;
document.write(alternateSort(str));
</script>
|
Output:
AbEdWddekkntuw
Time Complexity: O(n).
Auxiliary Space: O(max(n,26)), where n is the length of the string.
This is because when string is passed in the function it creates a copy of itself in stack.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...