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.
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++
#include <bits/stdc++.h>
using namespace std;
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 };
int number[] = { 6, 2, 5, 5, 4, 5, 6,
3, 7, 6 };
int countSticks(string str)
{
int cnt = 0;
for ( int i = 0; str[i]; i++) {
char ch = str[i];
if (ch >= 'A' && ch <= 'Z' ) {
cnt += sticks[ch - 'A' ];
}
else {
cnt += number[ch - '0' ];
}
}
return cnt;
}
int main()
{
string str = "GEEKSFORGEEKS" ;
cout << countSticks(str);
return 0;
}
|
Java
class GFG {
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 };
static int number[] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 ,
3 , 7 , 6 };
static int countSticks(String str)
{
int cnt = 0 ;
for ( int i = 0 ; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'A' && ch <= 'Z' ) {
cnt += sticks[ch - 'A' ];
}
else {
cnt += number[ch - '0' ];
}
}
return cnt;
}
public static void main (String[] args)
{
String str = "GEEKSFORGEEKS" ;
System.out.println(countSticks(str));
}
}
|
Python3
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 = [ 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ];
def countSticks(string) :
cnt = 0 ;
for i in range ( len (string)) :
ch = string[i];
if (ch > = 'A' and ch < = 'Z' ) :
cnt + = sticks[ ord (ch) - ord ( 'A' )];
else :
cnt + = number[ ord (ch) - ord ( '0' )];
return cnt;
if __name__ = = "__main__" :
string = "GEEKSFORGEEKS" ;
print (countSticks(string));
|
C#
using System;
class GFG
{
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 };
static int []number = { 6, 2, 5, 5, 4, 5, 6,
3, 7, 6 };
static int countSticks( string str)
{
int cnt = 0;
for ( int i = 0; i < str.Length; i++)
{
char ch = str[i];
if (ch >= 'A' && ch <= 'Z' )
{
cnt += sticks[ch - 'A' ];
}
else
{
cnt += number[ch - '0' ];
}
}
return cnt;
}
public static void Main()
{
string str = "GEEKSFORGEEKS" ;
Console.WriteLine(countSticks(str));
}
}
|
Javascript
<script>
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 ]
var number = [ 6, 2, 5, 5, 4, 5, 6,
3, 7, 6 ];
function countSticks(str)
{
var cnt = 0;
for ( var i = 0; str[i]; i++) {
var ch = str[i];
if (ch >= 'A' && ch <= 'Z' ) {
cnt += sticks[ch.charCodeAt(0) - 'A' .charCodeAt(0)];
}
else {
cnt += number[ch.charCodeAt(0) - '0' .charCodeAt(0)];
}
}
return cnt;
}
var str = "GEEKSFORGEEKS" ;
document.write(countSticks(str));
</script>
|
Time Complexity: O(N), where N is the length of given string.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...