Check Palindrome

def is_palindrome(s):
    l = len(s)

    for i in range(l // 2):
        if s[i] != s[l-i-1]:
            return False
    return True

#Challenge

SourceLanguageRuntime
DailyBytepython\(\mathcal{O}(n)\)

#Solution

For every pair of characters equidistant from the middle of the string, check whether the two characters match or not. If they don't then the string isn't a valid palendrome.

#Proof

Loop Invariant: At the end of each iteration of the loop, the characters \(i\) places from the start of the string is the same as the character at index \(i\) places from the end of the string, or the function returns False.

#Alternative Implementation

As shown in reversing a string python has a simple syntax to reverse collections. Using this we can create a cleaner answer by simply comparing our input string with its reversed value.

def is_palindrome(s):
    return s == s[::-1]

However it should be stated that this isn't an in-place solution. Python constructs a new array and copies the contents of s to it when reversing using the slice literal.