samedi 4 avril 2015

Reversing a string and palindrom time complexity in python

Is below code good for checking whether string is palindrome or not ? and what is its time complexity ? I guess O(1) Am i right ? Because we are just accessing same string with different index and so accessing index is O(1) operation. Please correct me if I am wrong. Please provide a better solution, if possible.



s1 = 'abccba'
s2 = s1[::-1]
if s1==s2:
print 'Palindrome'
else:
print 'Not Palindrome'

Aucun commentaire:

Enregistrer un commentaire