Instead of re - initialising boolean vector every time we just reset all visited letter to false
for j in range ( len ( s [ i ] ) ) : NEW_LINE INDENT visited [ ord ( s [ i ] [ j ] ) - ord ( ' a ' ) ] = False NEW_LINE DEDENT