Created
October 23, 2017 02:21
-
-
Save TT--/086a9da146a3850a3023140c2c19946f to your computer and use it in GitHub Desktop.
a string "contains" a word if a sub-sequence of the characters in the string can spell the word (characters must all appear and in the same order - takes q queries, each query consists of a string, For each query, print YES on a new line if string contains word; otherwise, print NO instead.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
import java.text.*; | |
import java.math.*; | |
import java.util.regex.*; | |
public class SubSequenceTest { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int q = in.nextInt(); | |
String hr = "hackerrank"; | |
wordloop : | |
for(int a0 = 0; a0 < q; a0++){ | |
String s = in.next(); | |
// your code goes here | |
int lastfound = -1; | |
int i; | |
for (i=0; i<10;i++){ | |
lastfound = s.indexOf(hr.charAt(i), lastfound+1); | |
//System.out.println(lastfound + " " +i); | |
if (lastfound < 0) { | |
Boolean contains = false; | |
System.out.println("NO"); | |
continue wordloop; | |
} | |
} | |
if ((lastfound > -1) && i==10) System.out.println("YES"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
note label
wordloop
and use ofcontinue label;
to deal with nested loops (otherwise No printed multiple times, andi
still increments.)