Given a string str of uppercase alphabets and numbers, the task is to find the number of matchsticks required to represent it.
Examples:
Input: str = “ABC2”
Output: 22
Explanation:
6 sticks are required to represent A,
7 sticks are required to represent B,
4 sticks are required to represent C,
5 sticks are required to represent 2.
Therefore the total number of matchsticks required is 6 + 7 + 4 + 5 = 22.Input: str = “GEEKSFORGEEKS”
Output: 66
Explanation:
6 sticks are required to represent G,
5 sticks are required to represent E,
4 sticks are required to represent K,
5 sticks are required to represent S,
4 sticks are required to represent F,
6 sticks are required to represent O,
6 sticks are required to represent R.
Therefore the total number of matchsticks required is 6 + 5 + 5 + 4 + 5 + 4 + 6 + 6 + 6 + 5 + 5 + 4 + 5 = 17.
Approach:
- The idea is to store the count of matchstick required to represent a particular alphabet and number as shown in above image.
- Traverse the given string str and add the count of matchstick required for each character.
Below is the implementation of the above approach:
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 |
66
Time Complexity: O(N), where N is the length of given string.
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.
Recommended Posts:
- Count of matchsticks required to represent the given number
- Minimum count of numbers required from given array to represent S
- Sort numbers based on count of letters required to represent them in words
- Minimize number of cuts required to break N length stick into N unit length sticks
- Count of lines required to write the given String
- Sort the strings based on the numbers of matchsticks required to represent them
- Count of minimum reductions required to get the required sum K
- Minimum number of given powers of 2 required to represent a number
- Count of integers up to N which represent a Binary number
- Count of ways to represent N as sum of a prime number and twice of a square
- Represent the fraction of two numbers in the string format
- Minimum number of given operations required to convert a string to another string
- Minimum changes required to make first string substring of second string
- Number of triangles possible with given lengths of sticks which are powers of 2
- Count operations of the given type required to reduce N to 0
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum rotations required to get the same String | Set-2
- Minimum rotations required to get the same string
- Count the number of carry operations required to add two numbers
- Minimum count of numbers required ending with 7 to sum as a given number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.
Improved By : AnkitRai01


