In this HackerRank in a String! the problem, For each query, print YES on a new line if the string contains hackerrank, otherwise, print NO.

HackerRank in a String! problem solution


Problem solution in Python programming.

#!/bin/python3

import sys

word ="hackerrank"
q = int(input().strip())
d = ()
for a0 in range(q):
    s = input().strip()
    n = len(s)
    # your code goes here
    p = 0
    wi = 0
    for wi in range(len(word)):
        if wi < 9:
            p = s.find(word[wi], p) +1
            if p == 0:
                break
    if wi == 9:
        print("YES")
    else:
        print("NO")


Problem solution in Java Programming.

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();
        String k = "hackerrank";
        for(int a0 = 0; a0 < q; a0++){
            String s = in.next();
            // your code goes here
            int index = 0;
            for (int i = 0; i < s.length(); i++) {
                if (index==8) {
                    break;
                }
                if (s.charAt(i)==k.charAt(index)) {
                    index++;
                }
            }
            if (index==8) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
    }
}


Problem solution in C++ programming.

#include <bits/stdc++.h>
#include <string>
using namespace std;

int main(){
    int q;
    cin >> q;
    for(int a0 = 0; a0 < q; a0++){
        string s;
        cin >> s;
        
        
        string nazwa="hackerrank";
        int l=0;
        bool xd=false;
        for (int i=0;i<s.length();i++)
            {
            if (s[i]==nazwa[l]) {if (l==8){xd=true;} else {l++;}}
            
        }
        if (xd==true) {cout<<"YES"<<endl;}
        else {cout<<"NO"<<endl;}
    }
    
    
    return 0;
}


Problem solution in C programming.

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

int main(){
    int q;
    char hackerrank[] = "hackerrank";
    scanf("%d",&q);
    for(int a0 = 0; a0 < q; a0++){
        char* s = (char *)malloc(512000 * sizeof(char));
        scanf("%s",s);
        
        // your code goes here
        int hr_i = 0;
        for(int i=0; s[i] != 0; ++i){
            if(hackerrank[hr_i] == s[i]) ++hr_i;
            if(hr_i == 10) break;
        }
        hr_i==10?printf("YES\n"):printf("NO\n");
    }
    return 0;
}


Problem solution in JavaScript programming.

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());
    var hr = 'hackerrank';
    var out = ''; var idx = 0;

    while (q > 0) {
      out = '';
      idx = 0;
      s = readLine();     
      if (s.length < hr.length) {
        console.log('NO');
        q--;
        continue;
      }
      for (var i = 0; i < s.length; i++) {
        idx = s.indexOf(hr[i], idx);
        if (idx < 0 ) break;
        out = out + s[idx];
        if (idx === s.length - 1) break;
        idx++;
      }
      out === hr ? console.log('YES') : console.log('NO');
      q--;
    }
}