Palindrome Index

Sort by

recency

|

1108 Discussions

|

  • + 0 comments

    Here is my solution. so before code this. i am trying to imagine how the palindrome works. and i found that. you only need to check first wrong character (first wrong from left, and first wrong from right)

    then what i do next ? I try to remove wrong character in left, check whether it is already palindrome. if no then i try to remove wrong character in right. if it is still not palindrome then there is no solution.

    public static int palindromeIndex(String s) {
    // Write your code here
        int leftWrongIndex = -1;
        int rightWrongIndex = -1;
        int sLength = s.length();
        for(int i=0; i < sLength/2; i++) {
            if(s.charAt(i) != s.charAt(sLength - 1 - i)) {
                leftWrongIndex = i;
                rightWrongIndex = sLength -1 -i;
                break;
            }
        }
        if(leftWrongIndex == -1 && rightWrongIndex == -1) {
            return -1;
        }
    
        if (checkPalindrome(s.substring(leftWrongIndex + 1, rightWrongIndex + 1))) {
            return leftWrongIndex;
        } else if (checkPalindrome(s.substring(leftWrongIndex, rightWrongIndex))) {
            return rightWrongIndex;
        }
    
        return -1;
    }
    
    public static boolean checkPalindrome(String s) {
        int sLength = s.length();
        for(int i=0; i< s.length() /2 ;i ++) {
            if(s.charAt(i) != s.charAt(sLength - 1 - i)) {
                return false;
            }
        }
        return true;
    }
    
  • + 0 comments

    Here is my C++ solution

    // helper function
    bool isPalindrome ( const string & str)
    {
        for (int i = 0; i < str.length() / 2; i++)
        { 
            if (str[i] != str[str.length() - i - 1])
                return false;
        }
        return true;
    } 
    
    int  palindromeIndex(string& s)
    {
      int backward = 0, forward = s.size() - 1;
    
      bool bPallindrome = true;
    
      while (backward < forward) 
      {
          if (s[backward] != s[forward]) 
          {
              bPallindrome = false;
              break;
          }
          backward++;
          forward--;
      }
      int result = -1;
    
      if (!bPallindrome)
      {
        result = isPalindrome(s.erase(forward, 1)) ? forward: backward;
      }
      return result;
    

    }

  • + 1 comment

    Can't for the life of me figure out what's wrong with my solution - it passes 12/15 test cases.

    def palindromeIndex(s):
            for i in range(len(s) // 2):
                    j = len(s) - 1 - i
                    if (s[i] == s[j]):
                            continue
                    if (s[i] == s[j - 1]):
                            return j
                    if (s[i + 1] == s[j]):
                            return i
            return -1
    
  • + 0 comments

    Python Complexity:O(n)

    def checkPalindrome(s):
        for i in range(len(s) // 2):
            if s[i] != s[len(s) - i - 1]:
                return False
        return True
    
    def palindromeIndex(s):
        idx = -1
        # s is palindrome
        if checkPalindrome(s):
            return -1
            
        # find the first character index that does not match
        for i in range(len(s) // 2):
            if s[i] == s[len(s) - i - 1]:
                continue
            else:
                idx = i
                break
        
        # drop the first character
        sub_s_1 = s[(idx+1) : (len(s) - idx)]
        if checkPalindrome(sub_s_1):
            return idx
        
        # drop the last character
        sub_s_2 = s[idx : (len(s) - idx - 1)]
        if checkPalindrome(sub_s_2):
            return len(s) - idx - 1
    
        return -1
    
  • + 0 comments
    def palindromeIndex(s):
        # Write your code here
        
        def isPalindrome(s, start, end):
            while start < end:
                if s[start] != s[end]:
                    return False
                start += 1
                end -= 1
            return True
        
        n = len(s)
        start = 0
        end = n - 1
        
        while start < end:
            if s[start] != s[end]:
                
                if isPalindrome(s, start + 1, end):
                    return start
                elif isPalindrome(s, start, end - 1):
                    return end
                else:
                    return -1
            start += 1
            end -= 1
                
        return -1