# 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.

Contents

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 = 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.