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:
- Rearrange a string in sorted order followed by the integer sum
- Rearrange the given string such that all prime multiple indexes have same character
- Rearrange characters to form palindrome if possible
- Modify the string such that every character gets replaced with the next character in the keyboard
- Replace every character of string by character whose ASCII value is K times more than it
- Find a string such that every character is lexicographically greater than its immediate next character
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Rearrange given string to maximize the occurrence of string t
- Map every character of one string to another such that all occurrences are mapped to the same character
- Replace every character of a string by a different character
- Rearrange a string so that all same characters become d distance away
- Rearrange characters in a string such that no two adjacent are same
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
- 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 using hashing
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.