Hackerrank - String - HackerRank in a String!
We say that a string, , contains the word
hackerrank if a subsequence of the characters in spell the word hackerrank. For example, haacckkerrannkk does contain hackerrank, but haacckkerannk does not (the characters all appear in the same order, but it's missing a second r).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.
You must answer queries, where each query consists of a string, . For each query, print
YES on a new line if contains hackerrank; otherwise, print NO instead.
Input Format
The first line contains an integer denoting (the number of queries).
Each line of the subsequent lines contains a single string denoting .
Each line of the subsequent lines contains a single string denoting .
Constraints
Output Format
For each query, print
YES on a new line if contains hackerrank; otherwise, print NO instead.
Sample Input 0
2
hereiamstackerrank
hackerworld
Sample Output 0
YES
NO
Explanation 0
We perform the following queries:
-
The characters ofhackerrankare bolded in the string above. Because the string contains all the characters inhackerrankin the same exact order as they appear inhackerrank, we printYESon a new line. - does not contain the last three characters of
hackerrank, so we printNOon a new line.
Solution
#!/bin/python
import sys
q = int(raw_input().strip())
for a0 in xrange(q):
st = raw_input().strip()
# your code goes here
main = "hackerrank"
g = 0
for k in main:
for d in range(1,len(st)+1):
if k == st[d-1:d]:
st = st[d:]
g += 1
break
if g == 10:
print "YES"
else:
print "NO"
Comments
Post a Comment