Palindrome Index

Sort by

recency

|

1094 Discussions

|

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

    Here is my solution in PHP

    s); arr); arr); res);

       `$resCount = count($`res);
       $result = -1;
       if($resCount == 1)
       {
        $result = -1;
       }
       else if(`$resCount == $`arrSum)
       {
        $result = -1;
       }
       else if(`$arr == $`arrRev)
       {
         $result = -1;
       }
       else
       {
        $equal = 'yes';
        $oddPair = '';
        $min = 0;
        foreach (`$res as $`key => $value) {
              if($value%2 !==0)
              {
                $equal = 'no';
                `$oddPair = $`key;
                if(`$value <$`min)
                {
                   `$min = $`value; 
                    `$oddPair = $`key;
                }
    
    
              }
        }
        `$len = count($`arr);
    
        if ($equal == 'no') {
    
             $findIndex = '';
             for (`$i=0; $`i < `$len ; $`i++) { 
                   if(`$arr[$`i] == $oddPair)
                   {
    
                     `$string = implode('', $`arr);
    
                           `$part1 = substr($`string, 0,$i);
                           `$part2 = substr($`string,`$i+1,$`len-1);
    
    
                         `$str1 = $`part1.$part2;
    
                         `$str2 = strrev($`str1);
    
                         if(`$str1 === $`str2)
                         {
    
                            `$findIndex =$`i;
                            break;
                         }
    
                   }
    
    
             }
             `$result = $`findIndex;
        }
    
       }
       return $result;
    
  • + 0 comments

    How is this easy ? WTF

  • + 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;
    }