Palindrome Index

Sort by

recency

|

1084 Discussions

|

  • + 0 comments
    function palindromeIndex(s) {
        let left = 0, right = s.length - 1
        while (left <= right) {
            if (s[left] !== s[right]) {
                if (isPalindrome(s.substring(left, right))) return right
                else if (isPalindrome(s.substring(left + 1, right + 1))) return left
                return -1
            } else {
                left++
                right--
            }
        }
        return -1
    }
    
    function isPalindrome(s) {
        for (let i = 0; i < Math.floor(s.length / 2); i++) {
            if (s[i] !== s[s.length - 1 - i]) return false
        }
        return true
    }
    
  • + 1 comment

    my code returning right answer but still showing wrong submission.

  • + 0 comments

    Here is my c++ solution : explanation here https://youtu.be/QjHpvMdfnqs,

     bool isPalindrome(string s){
         string r = s;
         reverse(r.begin(), r.end());
         return s == r;
     }
    
    int palindromeIndex(string input) {
        int s, e;
        for(s = 0, e = input.size() - 1; s < e; s++, e--){
            if(input[s] != input[e]) break;
        }
        if(s >= e) return -1;
        string s1 = input, s2 = input;
        s1.erase(s1.begin() + s);
        if(isPalindrome(s1)) return s;
        s2.erase(s2.begin() + e);
        if(isPalindrome(s2)) return e;
        return -1;
    }
    
  • + 0 comments

    // java solution

    private static int findIndexOfMismatch(String s) { for(int i=0 ; i

    public static int palindromeIndex(String s) {
        int mismatchIndex = findIndexOfMismatch(s);
        if(mismatchIndex==-1) {
            // is already a palindrome
            return -1;
        }
    
        // option 1 - remove mismatching char at the left and check
        String maybePalindrome = s.substring(mismatchIndex+1, s.length()-mismatchIndex);
        if(findIndexOfMismatch(maybePalindrome)==-1) {
            return mismatchIndex;
        }
    
        // option 2 - remove mismatching char at the right and check
        maybePalindrome = s.substring(mismatchIndex, s.length()-mismatchIndex-1);
        if(findIndexOfMismatch(maybePalindrome)==-1) {
            return s.length()-mismatchIndex-1;
        }
    
        // option 3 - removing one char is not enough to make this into a palindrome
        return -1;
    }
    
  • + 0 comments

    C# failed first tests using if(s == s,Reverse()) It was too slow for the tests - I'd still use it in production for readability though. Here's the final:

    public static int palindromeIndex(string s)
    {
        int stringLength = s.Length;            
        int returnPos = 0;
    
        if (IsReversible(s))
            return -1;
    
        for (int i = 0; i < stringLength/2; i++)
        {
            if (s[i] != s[stringLength-i-1])
            {
                returnPos = RemoveCharacterAndTest(s, i);
                if (returnPos>-1)
                    return returnPos;
            }
        }
        return -1;
    }
    
    private static int RemoveCharacterAndTest(string s, int removeAt)
    {
        string testString = s.Remove(removeAt,1);
        int returnPos = 0;
        if (IsReversible(testString))
            return removeAt;
    
        testString = s.Remove(s.Length - removeAt - 1, 1);
        if (IsReversible(testString))
        {
            returnPos = (s.Length - removeAt - 1);
            return returnPos;
        }                
        return -1;
    }
    
    private static bool IsReversible(string s)
    {
        int strLength = s.Length;
        for (int i = 0; i < strLength / 2; i++)
        {
            if (s[i] != s[strLength - i - 1])
                return false;
        }
        return true;
    }