Skip to content
TheCScience
TheCScience
  • Home
  • Human values
  • NCERT Solutions
  • HackerRank solutions
    • HackerRank Algorithms problems solutions
    • HackerRank C solutions
    • HackerRank C++ solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
TheCScience
HackerRank in a String! Problem Solution

HackerRank in a String! Problem Solution

Yashwant Parihar, April 24, 2023April 28, 2023

In this post, we will solve HackerRank in a String! Problem Solution.

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.
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 q, the number of queries.
Each of the next a lines contains a single query string s.

Sample Input 0

2
hereiamstackerrank
hackerworld

Sample Output 0

YES
NO

Explanation 0

We perform the following q = 2 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! Problem Solution
HackerRank in a String! Problem Solution

HackerRank in a String! C Solution

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

#define TRUE 1
#define FALSE 0

int main(){
    
    int check[10];
    for (int i = 0; i < 10; i++) {
        check[i] = FALSE;
        //printf("%d", check[i]);
    }
    
    int q; 
    scanf("%d",&q);
    for(int a0 = 0; a0 < q; a0++){
        char* s = (char *)malloc(512000 * sizeof(char));
        scanf("%s",s);
        
        for (int i = 0; s[i] != '\0'; i++) {
            if (s[i] == 'h' &&  check[0] == FALSE) {
                check[0] = TRUE; //printf("'h' found at s[%d]\n", i);                
            } else if ((s[i] == 'a') && (check[1] == FALSE) && (check[0] == TRUE)) {
                check[1] = TRUE; //printf("'a' found at s[%d]\n", i);                
            } else if ((s[i] == 'c') && (check[2] == FALSE) && (check[1] == TRUE)) {
                check[2] = TRUE; //printf("'c' found at s[%d]\n", i);
            } else if ((s[i] == 'k') && (check[3] == FALSE) && (check[2] == TRUE)) {
                check[3] = TRUE; //printf("'k' found at s[%d]\n", i);
            } else if ((s[i] == 'e') && (check[4] == FALSE) && (check[3] == TRUE)) {
                check[4] = TRUE; //printf("'e' found at s[%d]\n", i);
            } else if ((s[i] == 'r') && (check[5] == FALSE) && (check[4] == TRUE)) {
                check[5] = TRUE; //printf("'r' found at s[%d]\n", i);
            } else if ((s[i] == 'r') && (check[6] == FALSE) && (check[5] == TRUE)) {
                check[6] = TRUE; //printf("'r' found at s[%d]\n", i);
            } else if ((s[i] == 'a') && (check[7] == FALSE) && (check[6] == TRUE)) {
                check[7] = TRUE; //printf("'a' found at s[%d]\n", i);
            } else if ((s[i] == 'n') && (check[8] == FALSE) && (check[7] == TRUE)) {
                check[8] = TRUE; //printf("'n' found at s[%d]\n", i);
            } else if ((s[i] == 'k') && (check[9] == FALSE) && (check[8] == TRUE)) {
                check[9] = TRUE; //printf("'k' found at s[%d]\n", i); break;
            }
        }
        if (check[9] == TRUE && a0 != q) {
            printf("YES\n");
        } else if (check[9] == FALSE && a0 != q) {
            printf("NO\n");
        }
        if (check[9] == TRUE && a0 == q) {
            printf("YES");
        } else if (check[9] == FALSE && a0 == q) {
            printf("NO");
        }
        for (int i = 0; i < 10; i++) {
            check[i] = FALSE;
        }
    }
    return 0;
}

HackerRank in a String! C++ Solution

#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<string>

using namespace std;

int main() {
	int n,b=0,k=0;
	cin >> n;
	string s,h="hackerrank";
	vector<string>v;
	for (int i = 0; i < n; i++) {
		cin >> s;
		v.push_back(s);
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < v[i].size(); j++) {
			if (v[i][j] == h[k]) {
				k = k + 1;
				if (k == 10) {
					cout << "YES" << endl;;
					break;
				}
			}

		}
		if (k != 10) {
			cout << "NO" << endl;
		}
		k = 0;
	}
	return 0;
}

HackerRank in a String! C Sharp Solution

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

	public void subSequence(char[] mainString, char[] subString, int n, int m)
	{
		int x = 0;
		for (int i = 0; i < n && x < m; i++)
		{
			if (subString[x] == mainString[i])
				x++;
        }
		if (x == m)
			Console.WriteLine("YES");
		else
			Console.WriteLine("NO");
	}

	static void Main(String[] args)
	{
		Solution sol = new Solution();
		int q = Convert.ToInt32(Console.ReadLine());

		string p = "hackerrank";
		char[] sub = p.ToCharArray();
		for (int a0 = 0; a0 < q; a0++)
		{
			string s = Console.ReadLine();
			char[] main = s.ToCharArray();
			sol.subSequence(main, sub, main.Length, sub.Length);

		}
}
}

HackerRank in a String! Java Solution

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();
        
        char[] hr = "hackerrank".toCharArray();
        
        for(int a0 = 0; a0 < q; a0++){
            char[] test = in.next().toCharArray();
            int i = 0, j = 0;
            while (i < hr.length && j < test.length){
                if (hr[i] == test[j]){
                    i++;
                    j++;
                } else {
                    j++;
                }
            }
            if (i == hr.length) System.out.println("YES");
            else                System.out.println("NO");
        }
    }
}

HackerRank in a String! JavaScript Solution

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
  var q = parseInt(readLine());
  for(var a0 = 0; a0 < q; a0++){
    var s = readLine();
    var sArray = s.split('');
    // your code goes here
    check(sArray);
  }

}

function check(array) {
  var helperArray = [];
  var correctString = 'hackerrank';
  var correctArray = correctString.split('');
  var temp, n = 1; 
  var length = array.length;
  
  for (var i = 0; i < length; i++) {
    temp = array.pop();
    if (temp === correctString[correctString.length - n]) {
      n++;
      helperArray.unshift(temp);
    }
  }
  
  if (helperArray.length === correctArray.length) {
    console.log('YES')
  } else {
    console.log('NO')
  }
  
}

HackerRank in a String! Python Solution

#!/bin/python3

import sys


q = int(input().strip())
word = "hackerrank"
for a0 in range(q):
    s = input().strip()
    counter = 0
    for i in s:
        if (counter >= len(word)):
            break;
        if (i == word[counter]):
            counter += 1
    if (counter >= len(word)):
        print("YES")
    else:
         print("NO")   

Other Solutions

  • HackerRank Quicksort 1 – Partition Solution
  • HackerRank Pangrams Problem Solution
c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython

Post navigation

Previous post
Next post

Leave a Reply

You must be logged in to post a comment.

  • HackerRank Dynamic Array Problem Solution
  • HackerRank 2D Array – DS Problem Solution
  • Hackerrank Array – DS Problem Solution
  • Von Neumann and Harvard Machine Architecture
  • Development of Computers
©2025 TheCScience | WordPress Theme by SuperbThemes