loading...

Daily HackerRank Challenge - Day 5

wingkwong profile image Wing-Kam Updated on ・4 min read

Daily HackerRank Challenge (30 Part Series)

1) Daily HackerRank Challenge - Day 0 2) Daily HackerRank Challenge - Day 1 3 ... 28 3) Daily HackerRank Challenge - Day 2 4) Daily HackerRank Challenge - Day 3 5) Daily HackerRank Challenge - Day 4 6) Daily HackerRank Challenge - Day 5 7) Daily HackerRank Challenge - Day 6 8) Daily HackerRank Challenge - Day 7 9) Daily HackerRank Challenge - Day 8 10) Daily HackerRank Challenge - Day 9 11) Daily HackerRank Challenge - Day 10 12) Daily HackerRank Challenge - Day 11 13) Daily HackerRank Challenge - Day 12 14) Daily HackerRank Challenge - Day 13 15) Daily HackerRank Challenge - Day 14 16) Daily HackerRank Challenge - Day 15 17) Daily HackerRank Challenge - Day 16 18) Daily HackerRank Challenge - Day 17 19) Daily HackerRank Challenge - Day 19 20) Daily HackerRank Challenge - Day 20 21) Daily HackerRank Challenge - Day 21 22) Daily HackerRank Challenge - Day 22 23) Daily HackerRank Challenge - Day 23 24) Daily HackerRank Challenge - Day 24 25) Daily HackerRank Challenge - Day 25 26) Daily HackerRank Challenge - Day 26 27) Daily HackerRank Challenge - Day 27 28) Daily HackerRank Challenge - Day 28 29) Daily HackerRank Challenge - Day 29 30) Daily HackerRank Challenge - Day 30

About

This is a series of Daily HackerRank Challenge. Each day I show the some solutions written in C++.


HackerRank in a String

We say that a string contains the word hackerrank if a subsequence of its characters spell the word hackerrank. For example, if string s=haacckkerrannkk it does contain hackerrank, but s=haacckkerannk does not. In the second case, the second r is missing. If we reorder the first string as hccaakkerrannkk , it no longer contains the subsequence due to ordering.

More formally, let p[0], p[1],...,p[9] be the respective indices of h, a, c, k, e, r, r, a, n, k in string . If s is true, then p[0]<p[1]<p[2]< ... <p[9] contains hackerrank.

For each query, print YES on a new line if the string contains hackerrank, otherwise, print NO.

Sample Input

2
hereiamstackerrank
hackerworld

Sample Output

YES
NO

Read an input n and loop n times. Read another input s and loop s.size() times to check if the characher s[j] is equal to h[k] where h is the string hackerrank and k is the index of the character being checked starting from 0. If it matches, increase k by 1. At the end, if s contains hackerrank, k must be 10. Hence, print YES if so and NO if not.

Final Solution

int n,k;
string s,h="hackerrank";

int main()  
{ 
    FAST_INP;

    cin >> n;
    REP(i,n){
      cin >> s;
      k=0;
      REP(j, s.size()) {
          if(s[j]==h[k])k++;
      }
      cout << (k==10?"YES":"NO") << "\n";
    }

    return 0; 
} 

Mars Exploration

Letters in some of the SOS messages are altered by cosmic radiation during transmission. Given the signal received by Earth as a string,s, determine how many letters of Sami's SOS have been changed by radiation.

For example, Earth receives SOSTOT. Sami's original message was SOSSOS. Two of the message characters were changed in transit.

Sample Input

SOSSPSSQSSOR

Sample Output

3

Explanation
s = SOSSPSSQSSOR, and signal length |s|=12 . Sami sent 4 SOS messages (i.e.: 12/3=4).

Expected signal: SOSSOSSOSSOS
Recieved signal: SOSSPSSQSSOR
Difference: X X X

Read an input s and loop s times. Check each character if it matches the expected character.

1n character should match the character "S"
2n character should match the character "O"
3n character should match the character "S"

where n starts from 1 to |s|/3

We can declare a string SOS and get the expected character by using sos[i%3]. If it does not match, increase cnt by 1. Print cnt at the end.

Final Solution


string s;
string sos="SOS";
int cnt=0;

int main()  
{ 
    FAST_INP;

    cin >> s;
    REP(i,s.size()){
       if(s[i]!=sos[i%3])cnt++;
    }
    cout << cnt;
    return 0; 
} 

Pangrams

Roy wanted to increase his typing speed for programming contests. His friend suggested that he type the sentence "The quick brown fox jumps over the lazy dog" repeatedly. This sentence is known as a pangram because it contains every letter of the alphabet.

After typing the sentence several times, Roy became bored with it so he started to look for other pangrams.

Given a sentence, determine whether it is a pangram. Ignore case.

Sample Input 0

We promptly judged antique ivory buckles for the next prize

Sample Output 0

pangram

Sample Explanation 0

All of the letters of the alphabet are present in the string.

Sample Input 1

We promptly judged antique ivory buckles for the prize

Sample Output 1

not pangram

Sample Explanation 0

The string lacks an x.

Read a string s and loop s.size() times. We need to know if each letter exists in s. However, the input may contains a-z, A-Z, space, we need to convert it to lower case so that we can only use an array of 26 to solve this problem.

For example, if character a is found, we set a[0]=1, if character b is found, we set a[1]=1, and so on. Now we know each letter matches an index as below.

abcdef...z
012345...26

To find out which index to be used, we can use ASCII code. If you check out the table here, you should see the decimal of the letter a is 97, and b is 98 and so on. Hence, we can convert a character to an integer, which is the ASCII decimal of the letter, and subtracts 97 from it so that we can have the abovementioned index.

k=(int)tolower(s[i])-97;

Since it may include spaces, and the decimal of the space is 32. Hence, we need to take care of this case. If it is a space, you will get a negative value (32-97). We only need to set the value to 1 for a[i] if k is less than 0 and a[i] is 0. At the end, print out whether it is pangram or not pangram based on cnt.

Final Solution


string s;
int a[26]={0},k,cnt=0;

int main()  
{ 
    FAST_INP;

    getline(cin, s);
    REP(i,s.size()){
        k=(int)tolower(s[i])-97;
        if(k>=0 && a[k]==0) {
            ++a[k];
            ++cnt;
        }
    }
    cout << (cnt==26?"pangram":"not pangram") << "\n";
    return 0; 
} 

Complete Code

Check out the complete code via below links

Daily HackerRank Challenge (30 Part Series)

1) Daily HackerRank Challenge - Day 0 2) Daily HackerRank Challenge - Day 1 3 ... 28 3) Daily HackerRank Challenge - Day 2 4) Daily HackerRank Challenge - Day 3 5) Daily HackerRank Challenge - Day 4 6) Daily HackerRank Challenge - Day 5 7) Daily HackerRank Challenge - Day 6 8) Daily HackerRank Challenge - Day 7 9) Daily HackerRank Challenge - Day 8 10) Daily HackerRank Challenge - Day 9 11) Daily HackerRank Challenge - Day 10 12) Daily HackerRank Challenge - Day 11 13) Daily HackerRank Challenge - Day 12 14) Daily HackerRank Challenge - Day 13 15) Daily HackerRank Challenge - Day 14 16) Daily HackerRank Challenge - Day 15 17) Daily HackerRank Challenge - Day 16 18) Daily HackerRank Challenge - Day 17 19) Daily HackerRank Challenge - Day 19 20) Daily HackerRank Challenge - Day 20 21) Daily HackerRank Challenge - Day 21 22) Daily HackerRank Challenge - Day 22 23) Daily HackerRank Challenge - Day 23 24) Daily HackerRank Challenge - Day 24 25) Daily HackerRank Challenge - Day 25 26) Daily HackerRank Challenge - Day 26 27) Daily HackerRank Challenge - Day 27 28) Daily HackerRank Challenge - Day 28 29) Daily HackerRank Challenge - Day 29 30) Daily HackerRank Challenge - Day 30

Posted on by:

wingkwong profile

Wing-Kam

@wingkwong

Consultant by day. Developer by night. AWS certified. Exploring #CloudNative currently.

Discussion

markdown guide