HackerRank in a String! – HackerRank Solution

In this post, we will solve HackerRank in a String! HackerRank Solution. This problem (HackerRank in a String!) is a part of HackerRank Problem Solving series.

Task

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 hackerrank 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 = haacckkerrannk

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 q lines contains a single query string s.

Constraints

  • 2 <= q <= 102
  • 10 <= length of s <= 104

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

Solution – HackerRank in a String! – HackerRank Solution

C++

#include <bits/stdc++.h>

using namespace std;

string hackerrankInString(string s) {
    vector<char> word = {'k', 'n', 'a', 'r', 'r', 'e', 'k', 'c', 'a', 'h'};
    for (int i = 0; i < s.length(); i++) {
        if (word.size() == 0)
            break;
        else if (s[i] == word[word.size() - 1])
            word.pop_back();
    }
    
    if (word.size() == 0)
        return "YES";
    else
        return "NO";
}

int main() {
    int q;
    cin >> q;
    for(int a0 = 0; a0 < q; a0++){
        string s;
        cin >> s;
        string result = hackerrankInString(s);
        cout << result << endl;
    }
    return 0;
}

Python

import sys

def hackerrankInString(s):
    hckr = list('hackerrank')
    index = 0
    res = ''
    
    for let in s:
        if index == len(hckr):
            break
        if let == hckr[index]:
            index += 1
    
    if index == len(hckr):
        res = 'YES'
    else:
        res = 'NO'
    return res

if __name__ == "__main__":
    q = int(input().strip())
    for a0 in range(q):
        s = input().strip()
        result = hackerrankInString(s)
        print(result)

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();
        queries:
        for(int a0 = 0; a0 < q; a0++){
            String s = in.next();
            char[] find = "hackerrank".toCharArray(); 
            int findIndex = 0;
            
            for(char c : s.toCharArray())
            {
                if(find[findIndex] == c)
                    findIndex++;
                
                if(findIndex == find.length){ //We ran out of characters to find
                    System.out.println("YES");
                    continue queries;
                }
                    
            }
            System.out.println("NO"); //We didn't find all characters
        }
    }
}

Note: This problem (HackerRank in a String!) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *