HackerRank in a String!


We say that a string, s, contains the word hackerrank if a subsequence of the characters in s spell the word hackerrank. \n\nFor example, haacckkerrannkk does contain hackerrank, but haacckkerannk does not (the characters all appear in the same order, but it’s missing a second r).\n\nMore formally, let p0,p1,…,p9 be the respective indices of h, a, c, k, e, r, r, a, n, k in string s. If p0<p1<p2<..<p9 is true, then s contains hackerrank.\n\nYou must answer q queries, where each query i consists of a string, si. For each query, print YES on a new line if si contains hackerrank; otherwise, print NO instead.\n\nInput Format\n\nThe first line contains an integer denoting q(the number of queries). \nEach line i of the q subsequent lines contains a single string denoting si.\n\nConstraints\n2<=q<=10^2 \n\n10<=|si|<=10^4\n\nOutput Format\n\nFor each query, print YES on a new line if si contains hackerrank; otherwise, print NO instead.


import java.util.Scanner;

public class TestClass {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int q = scan.nextInt();
        while (q-- > 0) {
            String str = scan.next();
            System.out.println(subsequenceExists(str) ? "YES" : "NO");
    private static boolean subsequenceExists(String str) {
        String hackerrank = "hackerrank";
        int index = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == hackerrank.charAt(index)) {
            if (index == hackerrank.length()) {
                return true;
        return false;
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock