Palindrome Index

Sort by

recency

|

1105 Discussions

|

  • + 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
    
  • + 0 comments

    C++

    int palindromeIndex(string s)
    {
        for (size_t i = 0; i < s.length() / 2; i++)
        {
            if (s[i] != s[s.length() - 1 - i])
            {
                string tmp1 = s, tmp2 = s;
                tmp1.erase(i, 1), tmp2.erase(tmp2.length() - 1 - i, 1);
                
                reverse(tmp1.begin(), tmp1.begin() + tmp1.length() / 2);
                reverse(tmp2.begin(), tmp2.begin() + tmp2.length() / 2);
                
                if ((s.length() - 1) % 2)
                {
                    if (tmp1.substr(0, tmp1.length() / 2) == tmp1.substr(tmp1.length() / 2 + 1, tmp1.length() / 2))
                        return i;
                    else if (tmp2.substr(0, tmp2.length() / 2) == tmp2.substr(tmp2.length() / 2 + 1, tmp2.length() / 2))
                        return s.length() - 1 - i;
                    else
                        return -1;
                }
                else
                {
                    if (tmp1.substr(0, tmp1.length() / 2) == tmp1.substr(tmp1.length() / 2, tmp1.length() / 2))
                        return i;
                    else if (tmp2.substr(0, tmp2.length() / 2) == tmp2.substr(tmp2.length() / 2, tmp2.length() / 2))
                        return s.length() - 1 - i;
                    else
                        return -1;
                }
            }
        }
    
        return -1;
    }
    
  • + 0 comments

    My C++ Solution: Time complexity = O(n)

    string canPalindrome(string s){
        bool op1 = false;
            int slow = 1;
            int fast = s.length()-1;
            cout << s <<endl;
            while(fast > slow){
                if(s[fast] != s[slow]){
                    op1 = true;
                }
                fast --;
                slow++;
            } 
            if(op1)return "second";
            else return "first";
    }
    
    int palindromeIndex(string s) {
        int idx = -1;
        int fast = s.length()-1;
        int slow = 0;
        
        while(slow < fast){
            if(s[slow] != s[fast]){
                string sub = s.substr(slow,(fast-slow)+1);
                string result = canPalindrome(sub);
                result == "first" ? idx=slow : idx = fast;
                return idx;
            }
            slow++;
            fast--;
        }
        return idx;
    }
    
  • + 0 comments
    def palindrome(s):
        if s==s[::-1]:
            return True
        else:
            return False
            
    def palindromeIndex(s):
        if s==s[::-1]:
            return -1
        else:
            for i in range(len(s)//2):
                a=i
                b=len(s)-i-1
                if s[a]!=s[b]:
                    ns=s[:a]+s[a+1:]
                    ns2=s[:b]+s[b+1:]
                    if palindrome(ns):
                        return a
                    if palindrome(ns2):
                        return b