You are given two strings, AA and BB. Find if there is a substring that appears in both AA and BB.
All the strings contain only lowercase Latin letters.
Above, you see a question from hackerranck. I wrote the following program to solve it:
T = int(raw_input())
for t in xrange(T):
s1 = raw_input()
s2 = raw_input()
length1 = len(s1)
length2 = len(s2)
checked = list()
if length1<length2:
for letter in s1:
if len(checked)== 26:
break
if letter in checked:
next
checked.append(letter)
if letter in s2:
print "YES"
break
else:
print "NO"
else:
for letter in s2:
if letter in checked:
next
if len(checked)==26:
break
checked.append(letter)
if letter in s1:
print "YES"
break
else:
print "NO"
It worked correct before adding if len(checked)==26: break. I added this line to make it more efficient by checking each alphabetic letter only once and get rid of time-out error in the submitting process, but after adding this line, the answer of my program is wrong for some test cases. why?