Dada una string str de letras y números en mayúsculas, la tarea es encontrar el número de fósforos necesarios para representarla.
Ejemplos:
Entrada: str = “ABC2”
Salida: 22
Explicación:
Se requieren 6 palos para representar A,
7 palos para representar B,
4 palos para representar C,
5 palos para representar 2.
Por lo tanto, el número total de fósforos requerido es 6 + 7 + 4 + 5 = 22.
Entrada: str = “GEEKSFORGEEKS”
Salida: 66
Explicación:
Se requieren 6 palos para representar G,
5 palos para representar E,
4 palos para representar K,
5 palos se requieren para representar S,
se requieren 4 palos para representar F,
se requieren 6 palos para representar O,
Se requieren 6 palos para representar R.
Por lo tanto, el número total de fósforos necesarios es 6 + 5 + 5 + 4 + 5 + 4 + 6 + 6 + 6 + 5 + 5 + 4 + 5 = 17.
Acercarse:
- La idea es almacenar la cantidad de fósforos necesarios para representar un alfabeto y un número en particular, como se muestra en la imagen de arriba.
- Recorra la string str dada y agregue la cantidad de cerillas requeridas para cada carácter.
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; // stick[] stores the count // of matchsticks required to // represent the alphabets int sticks[] = { 6, 7, 4, 6, 5, 4, 6, 5, 2, 4, 4, 3, 6, 6, 6, 5, 7, 6, 5, 3, 5, 4, 6, 4, 3, 4 }; // number[] stores the count // of matchsticks required to // represent the numerals int number[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 }; // Function that return the count of // sticks required to represent // the given string int countSticks(string str) { int cnt = 0; // For every char of the given // string for (int i = 0; str[i]; i++) { char ch = str[i]; // Add the count of sticks // required to represent the // current character if (ch >= 'A' && ch <= 'Z') { cnt += sticks[ch - 'A']; } else { cnt += number[ch - '0']; } } return cnt; } // Driver code int main() { string str = "GEEKSFORGEEKS"; // Function call to find the // count of matchsticks cout << countSticks(str); return 0; }
Java
// Java implementation of the above approach class GFG { // stick[] stores the count // of matchsticks required to // represent the alphabets static int sticks[] = { 6, 7, 4, 6, 5, 4, 6, 5, 2, 4, 4, 3, 6, 6, 6, 5, 7, 6, 5, 3, 5, 4, 6, 4, 3, 4 }; // number[] stores the count // of matchsticks required to // represent the numerals static int number[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 }; // Function that return the count of // sticks required to represent // the given string static int countSticks(String str) { int cnt = 0; // For every char of the given // string for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); // Add the count of sticks // required to represent the // current character if (ch >= 'A' && ch <= 'Z') { cnt += sticks[ch - 'A']; } else { cnt += number[ch - '0']; } } return cnt; } // Driver code public static void main (String[] args) { String str = "GEEKSFORGEEKS"; // Function call to find the // count of matchsticks System.out.println(countSticks(str)); } } // This code is contributed by AnkitRai01
Python3
# Python3 implementation of the above approach # stick[] stores the count # of matchsticks required to # represent the alphabets sticks = [ 6, 7, 4, 6, 5, 4, 6, 5, 2, 4, 4, 3, 6, 6, 6, 5, 7, 6, 5, 3, 5, 4, 6, 4, 3, 4 ]; # number[] stores the count # of matchsticks required to # represent the numerals number = [ 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 ]; # Function that return the count of # sticks required to represent # the given string def countSticks(string) : cnt = 0; # For every char of the given # string for i in range(len(string)) : ch = string[i]; # Add the count of sticks # required to represent the # current character if (ch >= 'A' and ch <= 'Z') : cnt += sticks[ord(ch) - ord('A')]; else : cnt += number[ord(ch) - ord('0')]; return cnt; # Driver code if __name__ == "__main__" : string = "GEEKSFORGEEKS"; # Function call to find the # count of matchsticks print(countSticks(string)); # This code is contributed by AnkitRai01
C#
// C# implementation of the above approach using System; class GFG { // stick[] stores the count // of matchsticks required to // represent the alphabets static int []sticks = { 6, 7, 4, 6, 5, 4, 6, 5, 2, 4, 4, 3, 6, 6, 6, 5, 7, 6, 5, 3, 5, 4, 6, 4, 3, 4 }; // number[] stores the count // of matchsticks required to // represent the numerals static int []number = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 }; // Function that return the count of // sticks required to represent // the given string static int countSticks(string str) { int cnt = 0; // For every char of the given // string for (int i = 0; i < str.Length; i++) { char ch = str[i]; // Add the count of sticks // required to represent the // current character if (ch >= 'A' && ch <= 'Z') { cnt += sticks[ch - 'A']; } else { cnt += number[ch - '0']; } } return cnt; } // Driver code public static void Main() { string str = "GEEKSFORGEEKS"; // Function call to find the // count of matchsticks Console.WriteLine(countSticks(str)); } } // This code is contributed by AnkitRai01
Javascript
<script> // Javascript implementation of the // above approach // stick[] stores the count // of matchsticks required to // represent the alphabets var sticks = [ 6, 7, 4, 6, 5, 4, 6, 5, 2, 4, 4, 3, 6, 6, 6, 5, 7, 6, 5, 3, 5, 4, 6, 4, 3, 4 ] // number[] stores the count // of matchsticks required to // represent the numerals var number = [ 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 ]; // Function that return the count of // sticks required to represent // the given string function countSticks(str) { var cnt = 0; // For every char of the given // string for (var i = 0; str[i]; i++) { var ch = str[i]; // Add the count of sticks // required to represent the // current character if (ch >= 'A' && ch <= 'Z') { cnt += sticks[ch.charCodeAt(0) - 'A'.charCodeAt(0)]; } else { cnt += number[ch.charCodeAt(0) - '0'.charCodeAt(0)]; } } return cnt; } // Driver Code // Given array var str = "GEEKSFORGEEKS"; // Function Call document.write(countSticks(str)); // This code is contributed by ShubhamSingh10 </script>
66
Complejidad de tiempo: O(N), donde N es la longitud de la string dada.
Espacio Auxiliar: O(1)