Palindrome Index

Sort by

recency

|

1092 Discussions

|

  • + 0 comments
    by C:
    int palindromeIndex(char* s) {
    
        int len=0, left=0, right=len-1, tempLeft=0, tempRight=len-1;
        if(s != NULL) {
            while(s[len]!='\0') len++;
            right=len-1;
            tempRight=len-1;
    
            // normal checking
            while(left<right && (s[left]==s[right])) {
                left++;
                right--;
            }
            if(left>=right) return -1;
    
            // try to skip left
            tempLeft = left++;
            tempRight = right;
            while(left<right &&(s[left]==s[right])) {
                left++;
                right--;
            }
            if(left>=right) return tempLeft;
    
            // try to skip right ,BUT need first to back to the old value
            left = tempLeft;
            right = tempRight-1;
            while(left<right && (s[left]==s[right])) {
                left++;
                right--;
            }
            if(left>=right) return tempRight;
            else
                return -1; // so no solution for this string
    
        } else {
            return -1;
        }
    
    
    }
    
  • + 0 comments

    Perl solution:

    sub is_palindrome {
        my ($st, $left_s, $right_s) = @_;
        while ($left_s < $right_s) {
            return 0 if substr($st, $left_s, 1) ne substr($st, $right_s, 1);
            $left_s++;
            $right_s--;
        }
        return 1;
    }
    
    sub palindromeIndex {
        my $s = shift;
        my $left_side = 0;
        my $right_side = length($s) - 1;
    
        while ($left_side < $right_side) {
            if (substr($s, $left_side, 1) ne substr($s, $right_side, 1)) {
                if (is_palindrome($s, $left_side + 1, $right_side)) {
                    return $left_side;
                } elsif (is_palindrome($s, $left_side, $right_side - 1)) {
                    return $right_side;
                } else {
                    return -1;
                }
            }
            $left_side++;
            $right_side--;
        }
        return -1;
    }
    
  • + 0 comments

    My answer with Typescript, both worked.

    First attemp that simple

    function palindromeIndex1(s: string): number {
        // function check string [s] is palidrome
        const isPalindrome = (s: string): boolean => {
            for (let i = 0; i < Math.floor(s.length / 2); i++) {
                let ri = s.length - i - 1
                if (s[i] != s[ri]) return false
            }
            return true
        }
    
        // 1. check [s] isn't palindrome already
        if (!isPalindrome(s)) {
            // 2. loop [s]...
            for (let i = 0; i < s.length; i++)
                // 3. remove each character by [i], check remain [s] is palindrome
                if (isPalindrome(s.slice(0, i) + s.slice(i + 1)))
                    // 4. found palindrome, return [i]
                    return i
        }
    
        // 4. [s] alreay palidome, return -1
        return -1;
    }
    

    Second attemp is optimal

    function palindromeIndex(s: string): number {
        // function check string [s] is palidrome
        const isPalindrome = (start: number, end: number): boolean => {
            while (start < end) {
                if (s[start] != s[end]) return false;
                start++;
                end--;
            }
            return true;
        }
    
        /**
         * solution 1 works fine but it seems to be suboptimal.
         * new idea is define 2 index at start and end of [s], reduce to middle.
         * only check 2 case when non palindrome found
         * + case1: start removed
         * + case2: end removed
         * orelse non of [s] can be palindrome, return -1
         */
    
        // 1. define [start] and [end]
        let start = 0
        let end = s.length - 1
    
        // 2. loop ... recude [start] and [end] to middle
        while (start < end) {
            // 3. if [s] at [start] and [end] is not equal, mean not palidrome at [start] to [end]
            if (s[start] != s[end]) {
                // 4.1 try remove [start], check palidrome ... return if found
                if (isPalindrome(start + 1, end)) return start
                // 4.2 try remove [end], check palidrome ... return if found
                if (isPalindrome(start, end - 1)) return end
            }
            start++
            end--
        }
    
        // [start] and [end] go throuth [s] but palidrome not found, return -1
        return -1;
    }
    
  • + 0 comments
    def palindromeIndex(s):
        def is_palindrome(s, left, right):
            while left < right:
                if s[left] != s[right]:
                    return False
                left += 1
                right -= 1
            return True
    
        left, right = 0, len(s) - 1
        while left < right:
            if s[left] != s[right]:
                if is_palindrome(s, left + 1, right):
                    return left
                elif is_palindrome(s, left, right - 1):
                    return right
                else:
                    return -1
            left += 1
            right -= 1
        return -1  
    
  • + 1 comment
    def isPalindrome2(s):
        return s == s[::-1]
    
    def palindromeIndex2(s):
        if isPalindrome2(s):
            return -1
        else:
            for i in range(len(s) // 2 + 1):
                if s[i] != s[-1 - i]:
                    new_s1: str = s[0:i] + s[i + 1:]
                    new_s2: str = s[0:-1 - i] + s[-i:]
    
                    if isPalindrome2(new_s1):
                        return i
                    elif isPalindrome2(new_s2):
                        return len(s)-i-1
                    else:
                        return -1