Rearrange a string in the form of integer sum followed by the minimized character
Last Updated :
28 Oct, 2022
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 this manner: a+a = b, d+y = c.
Examples:
Input: str = "ab37b3a8"
Output: 21f
Sum of digits = 3 + 7 + 3 + 8 = 21
Sum of alphabets = a + b + b + a = 1 + 2 + 2 + 1 = 6
Alphabet at 6th position is f.
Input: str = "3by2b2a2"
Output: str = 9d
Approach: For separating digits and alphabets traverse the given string, if the character is numeric add it to digit-sum and if it alphabet add it to alphabet-sum.
- Start traversing the string.
- If digit is numeric add its value (str[i] - '0') to digitSum.
- Else add str[i]-'a'+1 to alphabetSum.
- Convert alphabetSum to char and add it to character format of digitSum
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
// function to return maximum volume
string separateChar(string str)
{
int n = str.size(), digitSum = 0;
int alphabetSum = 0, j = 0;
// separate digits and alphabets
for (int i = 0; i < n; i++) {
if (isdigit(str[i]))
digitSum += str[i] - '0';
else {
alphabetSum += str[i] - 'a' + 1;
alphabetSum %= 26;
}
}
// change digit sum to string
string sumStr = to_string(digitSum);
// change alphabet sum to string
char alphabetStr = char(alphabetSum + 'a' - 1);
// concatenate sum to alphabets string
sumStr += alphabetStr;
return sumStr;
}
// Driver code
int main()
{
string str = "3652adyz3423";
cout << separateChar(str);
return 0;
}
Java
// Java implementation of above approach
import java.util.*;
class Solution
{
// function to return maximum volume
static String separateChar(String str)
{
int n = str.length(), digitSum = 0;
int alphabetSum = 0, j = 0;
// separate digits and alphabets
for (int i = 0; i < n; i++) {
if (str.charAt(i)>='0'&&str.charAt(i)<='9') {
digitSum += (int)(str.charAt(i) - '0');
}
else {
alphabetSum += str.charAt(i) - 'a' + 1;
alphabetSum %= 26;
}
}
// change digit sum to string
String sumStr = ""+(digitSum);
// change alphabet sum to string
char alphabetStr = (char)(alphabetSum + 'a' - 1);
// concatenate sum to alphabets string
sumStr += alphabetStr;
return sumStr;
}
// Driver code
public static void main(String args[])
{
String str = "3652adyz3423";
System.out.println(separateChar(str));
}
}
//contributed by Arnab Kundu
Python3
# Python 3 implementation of the above approach
# function to return maximum volume
def separateChar(str__):
n = len(str__)
digitSum = 0
alphabetSum = 0
j = 0
# separate digits and alphabets
for i in range(n):
if (ord(str__[i]) >= 48 and
ord(str__[i]) <= 56):
digitSum += ord(str__[i]) - ord('0')
else:
alphabetSum += ord(str__[i]) - ord('a') + 1
alphabetSum %= 26
# change digit sum to string
sumStr = str(digitSum)
# change alphabet sum to string
alphabetStr = chr(alphabetSum + ord('a') - 1)
# concatenate sum to alphabets string
sumStr += alphabetStr
return sumStr
# Driver code
if __name__ == '__main__':
str__ = "3652adyz3423"
print(separateChar(str__))
# This code is contributed by
# Shashank_Sharma
C#
// C# implementation of above approach
using System;
public class Solution{
// function to return maximum volume
static String separateChar(String str)
{
int n = str.Length, digitSum = 0;
int alphabetSum = 0, j = 0;
// separate digits and alphabets
for (int i = 0; i < n; i++) {
if (str[i]>='0'&&str[i]<='9') {
digitSum += (int)(str[i] - '0');
}
else {
alphabetSum += str[i] - 'a' + 1;
alphabetSum %= 26;
}
}
// change digit sum to string
String sumStr = ""+(digitSum);
// change alphabet sum to string
char alphabetStr = (char)(alphabetSum + 'a' - 1);
// concatenate sum to alphabets string
sumStr += alphabetStr;
return sumStr;
}
// Driver code
public static void Main()
{
String str = "3652adyz3423";
Console.WriteLine(separateChar(str));
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript implementation of the above approach
// function to return maximum volume
function separateChar(str)
{
var n = str.length, digitSum = 0;
var alphabetSum = 0, j = 0;
// separate digits and alphabets
for (var i = 0; i < n; i++) {
if (str[i] >= '0' && str[i] <= '9')
digitSum += (str[i].charCodeAt(0) - '0'.charCodeAt(0));
else {
alphabetSum += (str[i].charCodeAt(0) - 'a'.charCodeAt(0) + 1);
alphabetSum %= 26;
}
}
// change digit sum to string
var sumStr = digitSum.toString();
// change alphabet sum to string
var alphabetStr = String.fromCharCode(alphabetSum + 'a'.charCodeAt(0) - 1);
// concatenate sum to alphabets string
sumStr += alphabetStr;
return sumStr;
}
// Driver code
var str = "3652adyz3423";
document.write( separateChar(str));
</script>
Time complexity: O(length(str))
Auxiliary space: O(1)
Similar Reads
Minimum cost to remove the spaces between characters of a String by rearranging the characters Given a string str that consists of characters and spaces, the task is to find the minimum cost to reduce the number of spaces between characters of the string. Cost of moving a character for index i to index j is defined as: | i - j | Examples: Input: str = " @ $" Output: 4 Explanation: As the char
7 min read
Minimize length of a string by removing suffixes and prefixes of same characters Given a string S of length N consisting only of characters 'a', 'b', and 'c', the task is to minimize the length of the given string by performing the following operations only once: Divide the string into two non-empty substrings and then, append the left substring to the end of the right substring
6 min read
Minimize characters to be changed to make the left and right rotation of a string same Given a string S of lowercase English alphabets, the task is to find the minimum number of characters to be changed such that the left and right rotation of the string are the same. Examples: Input: S = âabcdâOutput: 2Explanation:String after the left shift: âbcdaâString after the right shift: âdabc
8 min read
Minimum cost of flipping characters required to convert Binary String to 0s only Given binary string str, and integers A, which denotes the cost of converting consecutive 1s to 0s, and B, which denotes the cost of converting 0s to 1s. The task is to find the minimum cost to reduce the string str to 0s only. Examples: Input: str = â01101110â, A = 5, B = 1Output: 6Explanation:Conv
8 min read
Remove k characters in a String such that ASCII sum is minimum Given a string s and an integer k, the task is to remove k characters from the string such that the sum of ASCII (American Standard Code for Information Interchange) values of the remaining characters is minimized. Examples: Input: s = "ABbc", k = 2Output: 131Explanation: We need to remove exactly 2
7 min read