My issue
explain
Learning course: Strings using Python
Problem Link: CodeChef: Practical coding for everyone
explain
Learning course: Strings using Python
Problem Link: CodeChef: Practical coding for everyone
while i < (len(u)-1):
if u[i] == 't' and u[i+1] == 'h':
print('Yes')
break
i = i + 1
if i == (len(u) - 1):
print('no')
Explanation:
I am assuming you are familiar with basics of strings and loops.
Basically we are iterating through the string to check if any character in string is ‘t
’ AND its immediately followed by ‘h
’. We are using a while loop for this: if integer 'i'
is less than length of string ‘u
’ -1 we check if u[i]
is ‘t
’ AND u[i+1]
is ‘h
’ or not. If it is we print “Yes” and break out of loop, else we just increase ‘i’ and check for this new ‘i’.
Note we are looping from 0 to len(u)-2
(not len(u)-1
) as we are checking i
and i+1
(incase of len(u)-1
, it will give segmentation fault, as u[i+1] = u[len(u)]
doesn’t exist.)
Anyway, if we don’t find any 'th'
the loop will end with value of i = len(u)-1
. So if we have i == len(u)-1
, we print “No”
Let me know if anything confuses you.