You are viewing a single comment's thread. Return to all comments →
My Python solution doesn't pass many of the testcases! Can anyone help?
def ispalindrome(s): if s[::-1] == s: return True return False def palindromeIndex(s): s = list(s) if ispalindrome(s): return -1 for i in range(len(s) // 2 + 1): if ispalindrome(s[:i] + s[i + 1:]): return i if ispalindrome(s[:len(s) - i - 1] + s[len(s) - i:]): return len(s) - i - 1 return -1
I'm pretty sure that it gives the right answer, but it is not optimized.
Nevermind, adding a singular if statement somehow made it pass all the test cases!
def ispalindrome(s): if s[::-1] == s: return True return False def palindromeIndex(s): s = list(s) if ispalindrome(s): return -1 for i in range(len(s) // 2 + 1): if s[i] != s[-i - 1]: if ispalindrome(s[:i] + s[i + 1:]): return i if ispalindrome(s[:len(s) - i - 1] + s[len(s) - i:]): return len(s) - i - 1 return -1
Seems like cookies are disabled on this browser, please enable them to open this website
I agree to HackerRank's Terms of Service and Privacy Policy.
Palindrome Index
You are viewing a single comment's thread. Return to all comments →
My Python solution doesn't pass many of the testcases! Can anyone help?
I'm pretty sure that it gives the right answer, but it is not optimized.
Nevermind, adding a singular if statement somehow made it pass all the test cases!