Hackerrank Palindrome Index Solution

This is the solution to the problem Palindrome Index, which can be found in the Algorithm domain. The bellow solution is coded in python.

Screenshots



The Code
def isPal(s):
    for i in xrange(len(s)/2):
        if s[i] != s[len(s)-i-1]:
            return False
    return True
 
def solve(s):
    for i in xrange((len(s)+1)/2):
        if s[i] != s[len(s)-i-1]:
            if isPal(s[:i]+s[i+1:]):
                return i
            else:
                return len(s)-i-1
    return -1
for i in range(0,input()):
    x=raw_input()
    print solve(x)

Got any comments, drop them here !

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Hackerrank Modified Kaprekar Numbers Solution

Bit Stuffing Code Implementation in Java