Rearrange a string in sorted order followed by the integer sum
Last Updated :
15 Sep, 2023
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++
// C++ program for above implementation
#include<bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
// Function to return string in lexicographic
// order followed by integers sum
string arrangeString(string str)
{
int char_count[MAX_CHAR] = {0};
int sum = 0;
// Traverse the string
for (int i = 0; i < str.length(); i++)
{
// Count occurrence of uppercase alphabets
if (str[i]>='A' && str[i] <='Z')
char_count[str[i]-'A']++;
//Store sum of integers
else
sum = sum + (str[i]-'0');
}
string res = "";
// Traverse for all characters A to Z
for (int i = 0; i < MAX_CHAR; i++)
{
char ch = (char)('A'+i);
// Append the current character
// in the string no. of times it
// occurs in the given string
while (char_count[i]--)
res = res + ch;
}
// Append the sum of integers
if (sum > 0)
res = res + to_string(sum);
// return resultant string
return res;
}
// Driver program
int main()
{
string str = "ACCBA10D2EW30";
cout << arrangeString(str);
return 0;
}
Java
// Java program for above implementation
class Test
{
static final int MAX_CHAR = 26;
// Method to return string in lexicographic
// order followed by integers sum
static String arrangeString(String str)
{
int char_count[] = new int[MAX_CHAR];
int sum = 0;
// Traverse the string
for (int i = 0; i < str.length(); i++)
{
// Count occurrence of uppercase alphabets
if (Character.isUpperCase(str.charAt(i)))
char_count[str.charAt(i)-'A']++;
//Store sum of integers
else
sum = sum + (str.charAt(i)-'0');
}
String res = "";
// Traverse for all characters A to Z
for (int i = 0; i < MAX_CHAR; i++)
{
char ch = (char)('A'+i);
// Append the current character
// in the string no. of times it
// occurs in the given string
while (char_count[i]-- != 0)
res = res + ch;
}
// Append the sum of integers
if (sum > 0)
res = res + sum;
// return resultant string
return res;
}
// Driver method
public static void main(String args[])
{
String str = "ACCBA10D2EW30";
System.out.println(arrangeString(str));
}
}
Python3
# Python3 program for above implementation
MAX_CHAR = 26
# Function to return string in lexicographic
# order followed by integers sum
def arrangeString(string):
char_count = [0] * MAX_CHAR
s = 0
# Traverse the string
for i in range(len(string)):
# Count occurrence of uppercase alphabets
if string[i] >= "A" and string[i] <= "Z":
char_count[ord(string[i]) - ord("A")] += 1
# Store sum of integers
else:
s += ord(string[i]) - ord("0")
res = ""
# Traverse for all characters A to Z
for i in range(MAX_CHAR):
ch = chr(ord("A") + i)
# Append the current character
# in the string no. of times it
# occurs in the given string
while char_count[i]:
res += ch
char_count[i] -= 1
# Append the sum of integers
if s > 0:
res += str(s)
# return resultant string
return res
# Driver code
if __name__ == "__main__":
string = "ACCBA10D2EW30"
print(arrangeString(string))
# This code is contributed by
# sanjeev2552
C#
// C# program for above implementation
using System;
class GFG {
static int MAX_CHAR = 26;
// Method to return string in lexicographic
// order followed by integers sum
static String arrangeString(string str)
{
int []char_count = new int[MAX_CHAR];
int sum = 0;
// Traverse the string
for (int i = 0; i < str.Length; i++)
{
// Count occurrence of uppercase
// alphabets
if (char.IsUpper(str[i]))
char_count[str[i]-'A']++;
//Store sum of integers
else
sum = sum + (str[i]-'0');
}
string res = "";
// Traverse for all characters A to Z
for (int i = 0; i < MAX_CHAR; i++)
{
char ch = (char)('A' + i);
// Append the current character
// in the string no. of times it
// occurs in the given string
while (char_count[i]-- != 0)
res = res + ch;
}
// Append the sum of integers
if (sum > 0)
res = res + sum;
// return resultant string
return res;
}
// Driver method
public static void Main()
{
string str = "ACCBA10D2EW30";
Console.Write(arrangeString(str));
}
}
// This code is contributed by nitin mittal.
PHP
<?php
// PHP program for above implementation
$MAX_CHAR = 26;
// Function to return string in lexicographic
// order followed by integers sum
function arrangeString($str)
{
global $MAX_CHAR;
$char_count = array_fill(0, $MAX_CHAR, NULL);
$sum = 0;
// Traverse the string
for ($i = 0; $i < strlen($str); $i++)
{
// Count occurrence of uppercase alphabets
if ($str[$i] >= 'A' && $str[$i] <= 'Z')
$char_count[ord($str[$i]) -
ord('A')]++;
// Store sum of integers
else
$sum = $sum + (ord($str[$i]) -
ord('0'));
}
$res = "";
// Traverse for all characters A to Z
for ($i = 0; $i < $MAX_CHAR; $i++)
{
$ch = chr(ord('A') + $i);
// Append the current character
// in the string no. of times it
// occurs in the given string
while ($char_count[$i]--)
$res = $res . $ch;
}
// Append the sum of integers
if ($sum > 0)
$res = $res . strval($sum);
// return resultant string
return $res;
}
// Driver Code
$str = "ACCBA10D2EW30";
echo arrangeString($str);
// This code is contributed by ita_c
?>
JavaScript
<script>
// Javascript program for above implementation
let MAX_CHAR = 26;
// Method to return string in lexicographic
// order followed by integers sum
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;
// Traverse the string
for (let i = 0; i < str.length; i++)
{
// Count occurrence of uppercase alphabets
if (str[i] >= "A" && str[i] <= "Z")
char_count[str[i].charCodeAt(0)-
'A'.charCodeAt(0)]++;
//Store sum of integers
else
sum = sum + (str[i].charCodeAt(0)-
'0'.charCodeAt(0));
}
let res = "";
// Traverse for all characters A to Z
for (let i = 0; i < MAX_CHAR; i++)
{
let ch =
String.fromCharCode('A'.charCodeAt(0)+i);
// Append the current character
// in the string no. of times it
// occurs in the given string
while (char_count[i]-- != 0)
res = res + ch;
}
// Append the sum of integers
if (sum > 0)
res = res + sum;
// return resultant string
return res;
}
// Driver method
let str = "ACCBA10D2EW30";
document.write(arrangeString(str));
// This code is contributed by avanitrachhadiya2155
</script>
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Rearrange a string in the form of integer sum followed by the minimized character Given a string including lowercase alphabets and numeric digits. The task is to construct another string which consists of the sum of digits followed by the sum of all alphabets minimized to a single character. If no numeric digit is present add 0 to the string. Note: Alphabet summation is done in t
5 min read
Sort given sentence on the basis of integer present in every string Given a jumbled sentence as a list of strings, the task is to print the sorted sentence of strings on basis of the presence of a single integer in every string. If two string has the same integer sort them lexicographically. Examples: Input : {"2a", "grea3t", "l3earning", "geeksfor0geeks", "p10latfo
9 min read
Program to sort string in descending order Given a string, sort it in descending order. Examples: Input : alkasingh Output : snlkihgaa Input : nupursingh Output : uusrpnnihg Input : geeksforgeeks Output : ssrokkggfeeee Recommended PracticeSort the string in descending orderTry It! A simple solution is to use library sort function std::sort()
7 min read
Sort an array of strings in ascending order with each string sorted in descending order Given a array of strings S[] of size N (1 ? N ? 105), sort characters of each string in descending order and then print the array of strings in ascending order. Examples: Input: s[] = {"apple", "box", "cat"} Output: pplea tca xob Explanation: Sorting each string in descending order, S[] modifies to
9 min read
Sort a string in increasing order of given priorities 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 hav
8 min read