Code compiles without error in local IDE, flags "string index must be an integer error" at leetcode

  • -3
    __author__ = 'nsrivas3'

    import math

    class Solution:
    # @param {string} s
    # @return {string}
    def shortestPalindrome(self,s):
    # 1. Assume that the center of symmetry in the string is at the center of the string
    # 2. Verify to see if this is true
    # 3. Keep moving the center to the left till u can find symmetry somewhere
    # 4. As soon as we find symmetry we take the rest of the words in the string and put them in front of the first letter in the string
    # 5. This becomes our starting of the string
    def PalinCheck(str):
    len = str.len()
    if len%2==0:
    n = (len+1)/2-1
    m = (len+1)/2-1
    while (n>0):
    # Conditions only applied to the left part of the string since we shall be shifting the mean of the string
    # the left
    if str[math.floor(n)] != str[math.ceil(m)]:
    n = n - 1
    m = m + 1
    n = int((len+1)/2-1)
    i = 0
    while (n-i>=0):
    if str[int(n-i)] != str[int(n+i)]:
    i = i + 1
    str = s
    l = str.len()
    addStr = ""
    if (PalinCheck(str)==0):
    addStr = addStr + s[int(l-1)]
    else: return(addStr+s)
    l = l - 1
    str = str[0:l]
    # print(str)

    Sol1 = Solution()

  • 2

    Hi, could you first format your code properly ?

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.