-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathhackerrank_in_a_string.py
More file actions
42 lines (32 loc) · 917 Bytes
/
hackerrank_in_a_string.py
File metadata and controls
42 lines (32 loc) · 917 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/python3
"""
https://www.hackerrank.com/challenges/hackerrank-in-a-string/problem
"""
from collections import defaultdict
import os
def hackerrankInString(s):
target = 'hackerrank'
char_positions = defaultdict(list) # {char: [position_index_1, position_index_2, ]}
for i, char in enumerate(s):
if char in target:
char_positions[char].append(i)
last_index = -1
for char in target:
index = None
for i in char_positions[char]:
if i > last_index:
index = i
break
if index is None:
return 'NO'
else:
last_index = index
return 'YES'
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
q = int(input())
for _ in range(q):
s = input()
result = hackerrankInString(s)
fptr.write(result + '\n')
fptr.close()