FRom hackerrank
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.
Solution in C# -
class Solution {
static string hackerrankInString(string s) {
string s1 = "hackerrank";
if (s.Length < s1.Length) {
return "NO";
}
int j = 0;
for (int i = 0; i < s.Length; i++) {
if (j < s1.Length && s[i] == s1[j]) {
j++;
}
}
if(j==s1.Length)
return "YES";
else
return "NO";
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int q = Convert.ToInt32(Console.ReadLine());
for (int qItr = 0; qItr < q; qItr++) {
string s = Console.ReadLine();
string result = hackerrankInString(s);
textWriter.WriteLine(result);
}
textWriter.Flush();
textWriter.Close();
}
}

Post a Comment