HackerRank in a String!

Problem Statement :

We say that a string contains the word hackerrank if a subsequence of its characters spell the word hackerrank. Remeber 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 s. 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.

Function Description

Complete the hackerrankInString function in the editor below.

hackerrankInString has the following parameter(s):

string s: a string


string: YES or NO

Input Format

The first line contains an integer q, the number of queries.
Each of the next q lines contains a single query string s.


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

Solution :


                            Solution in C :

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]) {
        if (st == cur.size()) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        // your code goes here
    return 0;

In   Java  :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int q = in.nextInt();
        for(int a0 = 0; a0 < q; a0++){
            String s = in.next();
            // your code goes here
            System.out.println(hasSubseq(s,"hackerrank") ? "YES" : "NO"); 
    private static boolean hasSubseq(String source, String target) {
        if (target.length() == 0) return true; 
        if (source.length() == 0) return false;
        int x = 0; 
        for (char c : source.toCharArray()) {
            if (c == target.charAt(x)) 
            if (x == target.length()) 
                return true; 
        return false; 

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");
        return ("NO");

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

In   Python3  :


import sys
import re

q = int(input().strip())
for a0 in range(q):
    s = input().strip()
    pattern = ".*h.*a.*c.*k.*e.*r.*r.*a.*n.*k.*"
    m = re.search(pattern, s)
    if m is not None:

View More Similar Problems

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →


You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →

Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →