Given a string including lowercase alphabets and numeric digits. The task is to construct another string which consist of sum of digits followed by sum of all alphabets minimized to single character. If no numeric digit is present add 0 to string.
Note: Alphabet summation is done in this manner: a+a = b, d+y = c.
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Rearrange a string in sorted order followed by the integer sum
- String formed with middle character of every right substring followed by left sequentially
- Rearrange the given string such that all prime multiple indexes have same character
- Decode a string recursively encoded as count followed by substring
- Check if a string can be split into substrings starting with N followed by N characters
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Check If every group of a's is followed by a group of b's of same length
- Rearrange characters to form palindrome if possible
- Rearrange given string to maximize the occurrence of string t
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace every character of string by character whose ASCII value is K times more than it
- Replace every character of a string by a different character
- Map every character of one string to another such that all occurrences are mapped to the same character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Replace all occurrences of character X with character Y in given string
- Rearrange a string so that all same characters become d distance away
- Rearrange a string so that all same characters become atleast d distance away
- Rearrange characters in a string such that no two adjacent are same
- Rearrange a binary string as alternate x and y occurrences
- Rearrange the string to maximize the number of palindromic substrings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.