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(); for(int a0 = 0; a0 < q; a0++){ String s = in.next(); // your code goes here System.out.println(Solution.findSubsequence(s)? "YES": "NO"); } } public static boolean findSubsequence(String word){ String[] pattern = "hackerrank".split(""); boolean contains = false; HashMap charOccurences = new HashMap(); for(int x=0; x < pattern.length; x++){ Object count = charOccurences.get(pattern[x]); charOccurences.put(pattern[x], (count !=null? (int)count+1: 1)); } //there are 2 a's, 2 r's, 2 k's for(String character : pattern){ int numExpected = charOccurences.get(character); //check if the character occurs in the input string/word if(word.indexOf(character) > -1){ //check if there should be more than one character if(numExpected == 1){ contains = true; } else{ //check for subsequent occurence String substr = word.substring(word.indexOf(character)+1); //if found in the substring - then we are ok contains = (substr.indexOf(character) > -1); } } } //done searching for the sub-sequence - return yes/no return contains; } }