HackerRank in a String! - HackerRank Solution.
NOTE: If you are copying my code then its an advice to you to copy it after downloading it to avoid any kind of compilation error its link is available at the bottom of the source code.
We say that a string contains the word
hackerrank
if a subsequence of its characters spell the word hackerrank
. For example, if string it does contain hackerrank
, but does not. In the second case, the second r
is missing. If we reorder the first string as , it no longer contains the subsequence due to ordering.
More formally, let be the respective indices of
h
, a
, c
, k
, e
, r
, r
, a
, n
, k
in string . If is true, then contains hackerrank
.
For each query, print
YES
on a new line if the string contains hackerrank
, otherwise, print NO
.
Function Description
Complete the hackerrankInString function in the editor below. It must return
YES
or NO
.
hackerrankInString has the following parameter(s):
- s: a string
Input Format
The first line contains an integer , the number of queries.
Each of the next lines contains a single query string .
Each of the next lines contains a single query string .
Constraints
Output Format
For each query, print
YES
on a new line if contains hackerrank
, otherwise, print NO
.
Sample Input 0
2
hereiamstackerrank
hackerworld
Sample Output 0
YES
NO
Explanation 0
We perform the following queries:
The characters ofhackerrank
are bolded in the string above. Because the string contains all the characters inhackerrank
in the same exact order as they appear inhackerrank
, we printYES
on a new line.- does not contain the last three characters of
hackerrank
, so we printNO
on a new line.
Sample Input 1
2
hhaacckkekraraannk
rhbaasdndfsdskgbfefdbrsdfhuyatrjtcrtyytktjjt
Sample Output 1
YES
NO
SOURCE CODE
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the hackerrankInString function below.
static String hackerrankInString(String s) {
char ar[]={'h','a','c','k','e','r','r','a','n','k'};
int k=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)==ar[k])
{
k++;
if(k==ar.length)
break;
}
}
if(k==ar.length)
return ("YES");
else
return("NO");
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int q = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int qItr = 0; qItr < q; qItr++) {
String s = scanner.nextLine();
String result = hackerrankInString(s);
bufferedWriter.write(result);
bufferedWriter.newLine();
}
bufferedWriter.close();
scanner.close();
}
}
Click here to Download
RESULT
If you have any question then leave a comment below I will do my best to answer that question.
Comments
Post a Comment