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.
defcheckPalindrome(s):foriinrange(len(s)// 2):ifs[i]!=s[len(s)-i-1]:returnFalsereturnTruedefpalindromeIndex(s):idx=-1# s is palindromeifcheckPalindrome(s):return-1# find the first character index that does not matchforiinrange(len(s)// 2):ifs[i]==s[len(s)-i-1]:continueelse:idx=ibreak# drop the first charactersub_s_1=s[(idx+1):(len(s)-idx)]ifcheckPalindrome(sub_s_1):returnidx# drop the last charactersub_s_2=s[idx:(len(s)-idx-1)]ifcheckPalindrome(sub_s_2):returnlen(s)-idx-1return-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 →
Python Complexity:O(n)