Given a string str of uppercase alphabets and numbers, the task is to find the number of matchsticks required to represent it.
Input: str = “ABC2”
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”
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.
- 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:
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.
- 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 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.
Improved By : AnkitRai01