Rearrange a string in sorted order followed by the integer sum
Given a string containing uppercase alphabets and integer digits (from 0 to 9), the task is to print the alphabets in the order followed by the sum of digits.
Examples:
Input : AC2BEW3
Output : ABCEW5
Alphabets in the lexicographic order
followed by the sum of integers(2 and 3).
1- Start traversing the given string.
a) If an alphabet comes increment its
occurrence count into a hash_table.
b) If an integer comes then store it
separately by summing up everytime.
2- Using hash_table append all the
characters first into a string and
then at the end, append the integers
sum.
3- Return the resultant string.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
string arrangeString(string str)
{
int char_count[MAX_CHAR] = {0};
int sum = 0;
for ( int i = 0; i < str.length(); i++)
{
if (str[i]>= 'A' && str[i] <= 'Z' )
char_count[str[i]- 'A' ]++;
else
sum = sum + (str[i]- '0' );
}
string res = "" ;
for ( int i = 0; i < MAX_CHAR; i++)
{
char ch = ( char )( 'A' +i);
while (char_count[i]--)
res = res + ch;
}
if (sum > 0)
res = res + to_string(sum);
return res;
}
int main()
{
string str = "ACCBA10D2EW30" ;
cout << arrangeString(str);
return 0;
}
|
Java
class Test
{
static final int MAX_CHAR = 26 ;
static String arrangeString(String str)
{
int char_count[] = new int [MAX_CHAR];
int sum = 0 ;
for ( int i = 0 ; i < str.length(); i++)
{
if (Character.isUpperCase(str.charAt(i)))
char_count[str.charAt(i)- 'A' ]++;
else
sum = sum + (str.charAt(i)- '0' );
}
String res = "" ;
for ( int i = 0 ; i < MAX_CHAR; i++)
{
char ch = ( char )( 'A' +i);
while (char_count[i]-- != 0 )
res = res + ch;
}
if (sum > 0 )
res = res + sum;
return res;
}
public static void main(String args[])
{
String str = "ACCBA10D2EW30" ;
System.out.println(arrangeString(str));
}
}
|
Python3
MAX_CHAR = 26
def arrangeString(string):
char_count = [ 0 ] * MAX_CHAR
s = 0
for i in range ( len (string)):
if string[i] > = "A" and string[i] < = "Z" :
char_count[ ord (string[i]) - ord ( "A" )] + = 1
else :
s + = ord (string[i]) - ord ( "0" )
res = ""
for i in range (MAX_CHAR):
ch = chr ( ord ( "A" ) + i)
while char_count[i]:
res + = ch
char_count[i] - = 1
if s > 0 :
res + = str (s)
return res
if __name__ = = "__main__" :
string = "ACCBA10D2EW30"
print (arrangeString(string))
|
C#
using System;
class GFG {
static int MAX_CHAR = 26;
static String arrangeString( string str)
{
int []char_count = new int [MAX_CHAR];
int sum = 0;
for ( int i = 0; i < str.Length; i++)
{
if ( char .IsUpper(str[i]))
char_count[str[i]- 'A' ]++;
else
sum = sum + (str[i]- '0' );
}
string res = "" ;
for ( int i = 0; i < MAX_CHAR; i++)
{
char ch = ( char )( 'A' + i);
while (char_count[i]-- != 0)
res = res + ch;
}
if (sum > 0)
res = res + sum;
return res;
}
public static void Main()
{
string str = "ACCBA10D2EW30" ;
Console.Write(arrangeString(str));
}
}
|
PHP
<?php
$MAX_CHAR = 26;
function arrangeString( $str )
{
global $MAX_CHAR ;
$char_count = array_fill (0, $MAX_CHAR , NULL);
$sum = 0;
for ( $i = 0; $i < strlen ( $str ); $i ++)
{
if ( $str [ $i ] >= 'A' && $str [ $i ] <= 'Z' )
$char_count [ord( $str [ $i ]) -
ord( 'A' )]++;
else
$sum = $sum + (ord( $str [ $i ]) -
ord( '0' ));
}
$res = "" ;
for ( $i = 0; $i < $MAX_CHAR ; $i ++)
{
$ch = chr (ord( 'A' ) + $i );
while ( $char_count [ $i ]--)
$res = $res . $ch ;
}
if ( $sum > 0)
$res = $res . strval ( $sum );
return $res ;
}
$str = "ACCBA10D2EW30" ;
echo arrangeString( $str );
?>
|
Javascript
<script>
let MAX_CHAR = 26;
function arrangeString(str)
{
let char_count = new Array(MAX_CHAR);
for (let i=0;i<MAX_CHAR;i++)
{
char_count[i]=0;
}
let sum = 0;
for (let i = 0; i < str.length; i++)
{
if (str[i] >= "A" && str[i] <= "Z" )
char_count[str[i].charCodeAt(0)-
'A' .charCodeAt(0)]++;
else
sum = sum + (str[i].charCodeAt(0)-
'0' .charCodeAt(0));
}
let res = "" ;
for (let i = 0; i < MAX_CHAR; i++)
{
let ch =
String.fromCharCode( 'A' .charCodeAt(0)+i);
while (char_count[i]-- != 0)
res = res + ch;
}
if (sum > 0)
res = res + sum;
return res;
}
let str = "ACCBA10D2EW30" ;
document.write(arrangeString(str));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
15 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...