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:
# Python 3 implementation of the above approach
# function to return maximum volume
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 [tabby title="C#"]
- Rearrange a string in sorted order followed by the integer sum
- Rearrange characters to form palindrome if possible
- 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
- 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
- Replace every character of a string by a different character
- Rearrange characters in a string such that no two adjacent are same
- Rearrange a string so that all same characters become d distance away
- Rearrange a binary string as alternate x and y occurrences
- 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
- Minimal moves to form a string by adding characters or appending string itself
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Find the count of palindromic sub-string of a string in its sorted form
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.