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++
#include <bits/stdc++.h>
using namespace std;
string separateChar(string str)
{
int n = str.size(), digitSum = 0;
int alphabetSum = 0, j = 0;
for ( int i = 0; i < n; i++) {
if ( isdigit (str[i]))
digitSum += str[i] - '0' ;
else {
alphabetSum += str[i] - 'a' + 1;
alphabetSum %= 26;
}
}
string sumStr = to_string(digitSum);
char alphabetStr = char (alphabetSum + 'a' - 1);
sumStr += alphabetStr;
return sumStr;
}
int main()
{
string str = "3652adyz3423" ;
cout << separateChar(str);
return 0;
}
|
Java
import java.util.*;
class Solution
{
static String separateChar(String str)
{
int n = str.length(), digitSum = 0 ;
int alphabetSum = 0 , j = 0 ;
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 ;
}
}
String sumStr = "" +(digitSum);
char alphabetStr = ( char )(alphabetSum + 'a' - 1 );
sumStr += alphabetStr;
return sumStr;
}
public static void main(String args[])
{
String str = "3652adyz3423" ;
System.out.println(separateChar(str));
}
}
|
Python3
def separateChar(str__):
n = len (str__)
digitSum = 0
alphabetSum = 0
j = 0
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
sumStr = str (digitSum)
alphabetStr = chr (alphabetSum + ord ( 'a' ) - 1 )
sumStr + = alphabetStr
return sumStr
if __name__ = = '__main__' :
str__ = "3652adyz3423"
print (separateChar(str__))
|
C#
using System;
public class Solution{
static String separateChar(String str)
{
int n = str.Length, digitSum = 0;
int alphabetSum = 0, j = 0;
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;
}
}
String sumStr = "" +(digitSum);
char alphabetStr = ( char )(alphabetSum + 'a' - 1);
sumStr += alphabetStr;
return sumStr;
}
public static void Main()
{
String str = "3652adyz3423" ;
Console.WriteLine(separateChar(str));
}
}
|
Javascript
<script>
function separateChar(str)
{
var n = str.length, digitSum = 0;
var alphabetSum = 0, j = 0;
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;
}
}
var sumStr = digitSum.toString();
var alphabetStr = String.fromCharCode(alphabetSum + 'a' .charCodeAt(0) - 1);
sumStr += alphabetStr;
return sumStr;
}
var str = "3652adyz3423" ;
document.write( separateChar(str));
</script>
|
Time complexity: O(length(str))
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...