We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def palindromeIndex(s):
def if_pand(s):
left = 0
right = len(s) - 1
while left < right:
if s[left] != s[right]:
return left,right
left += 1
right -= 1
return -1,-1
r_right,r_left = if_pand(s)
if r_left != -1 and r_right != -1:
#remove left index first
tmp_s = s[0:r_left] + s[r_left+1:]
tmp1, tmp2 = if_pand(tmp_s)
if tmp1 == -1 and tmp2 == -1:
return r_left
else:
#try to remove right index
tmp_s = s[0:r_right] + s[r_right+1:]
tmp1, tmp2 = if_pand(tmp_s)
if tmp1 == -1 and tmp2 == -1:
return r_right
return -1
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Palindrome Index
You are viewing a single comment's thread. Return to all comments →
my python code pass all the tests
def palindromeIndex(s): def if_pand(s): left = 0 right = len(s) - 1