HackerRank in a String! HackerRank Solution in C, C++, Java, Python

We say that a string contains the word hackerrank if a subsequence of its characters spell the word hackerrank. Remember that a subsequence maintains the order of characters selected from a sequence.

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 p[0]<p[1]<p[2]<…p[9] is true, then s contains hackerrank.

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

Example

s=haacckkerrannkk

This contains a subsequence of all of the characters in the proper order. Answer YES

s=haacckkerannk

This is missing the second ‘r’. Answer NO.

s=hccaakkerrannkk

There is no ‘c’ after the first occurrence of an ‘a’, so answer NO.

Function Description

Complete the hackerrankInString function in the editor below.

hackerrankInString has the following parameter(s):

  • string s: a string

Returns

  • string: YES or NO

Input Format

The first line contains an integer , the number of queries.

Each of the next  lines contains a single query string .

Constraints

  • 1<=q<=10^2
  • 10<=length of s<=10^4

Sample Input 0

2

hereiamstackerrank

hackerworld


Sample Output 0

YES

NO

 

Explanation 0

We perform the following  queries:

  1. s=hereiamstackerrank
    The characters of hackerrank are bolded in the string above. Because the string contains all the characters in hackerrank in the same exact order as they appear in hackerrank, we return YES.
  2. s=hackerworld does not contain the last three characters of hackerrank, so we return NO.

Sample Input 1

2

hhaacckkekraraannk

rhbaasdndfsdskgbfefdbrsdfhuyatrjtcrtyytktjjt

 

Sample Output 1

YES

NO

 

HackerRank in a String! HackerRank Solution in C

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

char *hackerrank(char *string)
{
    int total;
    int i;
    int j;    
    char *comp = "hackerrank\0";
    
    i = 0;
    j = 0;
    while (string[i] != '\0')
    {
        if (string[i] == comp[j])
            j += 1;
        i += 1;
    }
    if (strlen(comp) == j)
        return ("YES");
    else
        return ("NO");
}

int main(){
    int q; 
    scanf("%d",&q);
    for(int a0 = 0; a0 < q; a0++){
        char* s = (char *)malloc(512000 * sizeof(char));
        scanf("%s",s);
        printf("%s\n", hackerrank(s));
        // your code goes here
    }
    return 0;
}

 

HackerRank in a String! HackerRank Solution in C++

#include <bits/stdc++.h>

using namespace std;

int main(){
    int q;
    cin >> q;
    for(int a0 = 0; a0 < q; a0++){
        string s;
        cin >> s;
        string cur = "hackerrank";
        int st = 0;
        for (int i= 0; i < s.size() && st < cur.size(); i++) {
            if (s[i] == cur[st]) {
                st++;
            }
        }
        if (st == cur.size()) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
        // your code goes here
    }
    return 0;
}

 

HackerRank in a String! HackerRank Solution in Java

import java.util.Scanner;

public class LCS {

    public static void main(String[] args) throws Exception {

        Scanner scan = new Scanner(System.in);

        int q = scan.nextInt();
        while (q-- > 0) {
            String s1 = scan.next();
            String s2 = "hackerrank";
            int m = s1.length();
            int n = s2.length();

            char a[] = s1.toCharArray();
            char b[] = s2.toCharArray();

            int c[][] = new int[n + 1][m + 1];

            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    if (b[i - 1] == a[j - 1]) {
                        c[i][j] = c[i - 1][j - 1] + 1;
                    } else {
                        c[i][j] = Math.max(c[i - 1][j], c[i][j - 1]);
                    }
                }
            }
            int count = c[n][m];
            if(count == n) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
    }
}

 

HackerRank in a String! HackerRank Solution in Python

#!/bin/python

import sys


q = int(raw_input().strip())
for a0 in xrange(q):
    s = raw_input().strip()
    a="hackerrank"
    c=0
    for i in s:
        if i==a[c]:
            c+=1
        if c>=10:
            break
    if c==10:
        print "YES"
    else:
        print "NO"
    
    # your code goes here

 

HackerRank in a String! HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{

    static void Main(String[] args)
    {
        int q = Convert.ToInt32(Console.ReadLine());
        for (int a0 = 0; a0 < q; a0++)
        {
            char[] hr = new char[10] { 'h', 'a', 'c', 'k', 'e', 'r', 'r', 'a', 'n', 'k' };
            char[] sArray = Console.ReadLine().ToCharArray();
            bool status = true;
            for (int i = 0; i < hr.Length; i++)
            {
                char symbol = hr[i];
                int index = Array.FindIndex(sArray, s => s == symbol);
                if (index < 0)
                {
                    status = false;
                    break;
                }
                var newArr = new char[sArray.Length - index - 1];
                Array.Copy(sArray, index+1, newArr, 0, sArray.Length-index-1);
                sArray = newArr;
            }
            Console.WriteLine(status?"YES":"NO");
        }
    }
}

 

Attempt HackerRank in a String! HackerRank Challenge

Link – https://www.hackerrank.com/challenges/alternating-characters/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/quicksort-1-partition-hackerrank-solution/

Leave a Comment