Saturday, 14 March 2015

Hackerearth What is the string made of? solution

PROBLEM STATEMENT:

You are given a string, which contains entirely of decimal digits (0-9). Each digit is made of a certain number of dashes, as shown in the image below. For instance 1 is made of 2 dashes, 8 is made of 7 dashes and so on.
digits made of dashes
You have to write a function that takes this string message as an input and returns a corresponding value in terms of a number. This number is the count of dashes in the string message.
Note:
0 consists of 6 dashes, 1 consists of 2 dashes, 2 consists of 5 dashes, 3 consists of 5 dashes, 4 consists of 4 dashes, 5 consists of 5 dashes, 6 consists of 6 dashes, 7 consists of 3 dashes [though the figure shows that 7 consists of 4 dashes but due to minor mistake in the problem please write your solution assuming 7 consists of 3 dashes], 8 consists of 7 dashes, 9 consists of 6 dashes.
Constraints
  • String message will contain at least one digit, but not more than 100
  • Each character in code will be a digit ('0'-'9').

SOURCE CODE:

#include <iostream>
#include <string.h>
using namespace std;  
 int main() {  
      int arr[100]={6,2,5,5,4,5,6,3,7,6},ans = 0;  
      char str[10000] ;  
      cin>>str;  
      for(int i=0;str[i]!='\0';i++)  
   {  
     int temp = str[i] -48 ;  
     ans += arr[temp];  
   }  
      cout<<ans<<endl;  
      return 0;  
 }  
** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerearth. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

2 comments:

  1. The ingredients of string are mentioned in this blog. We will easily find the things that what is string made of. This is a different blog from dissertationteam testimonials developmental work. I am really very sure about the creativity of blogging and some other activities.

    ReplyDelete
  2. I am so interested to read your topic but it is very useful for me and my dissertation writing service

    ReplyDelete