Dada una string que incluye letras en minúsculas y dígitos numéricos. La tarea es construir otra string que consista en la suma de dígitos seguida de la suma de todos los alfabetos minimizados a un solo carácter. Si no hay ningún dígito numérico presente, agregue 0 a la string.
Nota: La suma alfabética se realiza de esta manera: a+a = b, d+y = c.
Ejemplos:
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
Enfoque: para separar dígitos y alfabetos, recorra la string dada, si el carácter es numérico, agréguelo a digit-sum y si es un alfabeto, agréguelo a alphabet-sum.
- Comience a atravesar la cuerda.
- Si el dígito es numérico, agregue su valor (str[i] – ‘0’) a digitSum.
- De lo contrario, agregue str[i]-‘a’+1 a alphabetSum.
- Convierta alphabetSum a char y agréguelo al formato de caracteres de digitSum .
A continuación se muestra la implementación del enfoque anterior:
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>
28d
Publicación traducida automáticamente
Artículo escrito por Shivam.Pradhan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA