Open In App

Rearrange a string in the form of integer sum followed by the minimized character

Last Updated : 28 Oct, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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. 
 

  1. Start traversing the string.
  2. If digit is numeric add its value (str[i] - '0') to digitSum.
  3. Else add str[i]-'a'+1 to alphabetSum.
  4. 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> 

Output: 
28d

 

Time complexity: O(length(str))
Auxiliary space: O(1)


Next Article
Practice Tags :

Similar Reads