I am trying to print "YES" or "NO" (quotes for clarity) depending on whether any permutation of the pattern exists in the text string.
from itertools import permutations
i = int(input())
for j in range(i):
m = input()
n = input()
lst = []
for k in permutations(m):
lst.append("".join(k))
for l in lst:
if l in n:
print("YES")
break
else:
print("NO")
break
print(lst)
This is the sample input:
3
hack
indiahacks
code
eddy
coder
iamredoc
This is the expected output
YES
NO
YES
For first two inputs, output is coming correctly and the third output is the one which fails.
/home/ajay/.pyenv/versions/3.4.1/bin/python3.4 /home/ajay/PycharmProjects/LearnPython/test.py
1
coder
iamredoc
NO
['coder', 'codre', 'coedr', 'coerd', 'corde', 'cored', 'cdoer', 'cdore', 'cdeor', 'cdero', 'cdroe', 'cdreo', 'ceodr', 'ceord', 'cedor', 'cedro', 'cerod', 'cerdo', 'crode', 'croed', 'crdoe', 'crdeo', 'creod', 'credo', 'ocder', 'ocdre', 'ocedr', 'ocerd', 'ocrde', 'ocred', 'odcer', 'odcre', 'odecr', 'oderc', 'odrce', 'odrec', 'oecdr', 'oecrd', 'oedcr', 'oedrc', 'oercd', 'oerdc', 'orcde', 'orced', 'ordce', 'ordec', 'orecd', 'oredc', 'dcoer', 'dcore', 'dceor', 'dcero', 'dcroe', 'dcreo', 'docer', 'docre', 'doecr', 'doerc', 'dorce', 'dorec', 'decor', 'decro', 'deocr', 'deorc', 'derco', 'deroc', 'drcoe', 'drceo', 'droce', 'droec', 'dreco', 'dreoc', 'ecodr', 'ecord', 'ecdor', 'ecdro', 'ecrod', 'ecrdo', 'eocdr', 'eocrd', 'eodcr', 'eodrc', 'eorcd', 'eordc', 'edcor', 'edcro', 'edocr', 'edorc', 'edrco', 'edroc', 'ercod', 'ercdo', 'erocd', 'erodc', 'erdco', 'erdoc', 'rcode', 'rcoed', 'rcdoe', 'rcdeo', 'rceod', 'rcedo', 'rocde', 'roced', 'rodce', 'rodec', 'roecd', 'roedc', 'rdcoe', 'rdceo', 'rdoce', 'rdoec', 'rdeco', 'rdeoc', 'recod', 'recdo', 'reocd', 'reodc', 'redco', 'redoc']
Process finished with exit code 0
You can see that redoc is present in the list of permutations but why the python in keyword is not recognizing it and thus producing NO output.