Skip to content

Instantly share code, notes, and snippets.

@TT--
Created October 23, 2017 02:21
Show Gist options
  • Save TT--/086a9da146a3850a3023140c2c19946f to your computer and use it in GitHub Desktop.
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.
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");
}
}
}
@TT--
Copy link
Author

TT-- commented Oct 23, 2017

note label wordloop and use of continue label; to deal with nested loops (otherwise No printed multiple times, and i still increments.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment