Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Count of sticks required to represent the given string

  • Last Updated : 29 Nov, 2021

Given a string str of uppercase alphabets and numbers, the task is to find the number of matchsticks required to represent it.
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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: 
 

  1. The idea is to store the count of matchstick required to represent a particular alphabet and number as shown in above image.
  2. 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

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>
Output: 
66

 

Time Complexity: O(N), where N is the length of given string.

Auxiliary Space: O(1)
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!