Palindrome Index

Sort by

recency

|

1087 Discussions

|

  • + 0 comments

    Has anyone managed to pass all the test using Rust? I'm hitting the time limit on some tests and I'm pretty sure I got a really optimized O(n), zero copy solution (with help from solutions below in other languages).

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

     public static boolean isPalindrome(String s){
        int start = 0, end = s.length() - 1;
         while(start < end){
          if(s.charAt(start) != s.charAt(end))
            return false;
          start++;
          end--;
          } 
        return true;
        }
      
      public static int palindromeIndex(String s) {
        int start = 0, end = s.length() - 1;
    
        if(isPalindrome(s))
          return -1;
        
        while(start < end){
          int start_elem = s.charAt(start);
          int end_elem = s.charAt(end);
          
          if(start_elem != end_elem){
            String s_without_start = s.substring(start + 1, end + 1);
            if(isPalindrome(s_without_start))
              return start;
          
            String s_without_end = s.substring(start, end);
            if(isPalindrome(s_without_end))
              return end;  
            }
          start++;
          end--;
          }
        
        return -1; 
      }
    
  • + 0 comments

    Below is my C# solution with explaination in comments. Hope you find it useful.

    public static int palindromeIndex(string s)
    {
        for (int i = 0, j = s.Length - 1; i < j; i++, j--)
        {
            if (s[i] != s[j])
            {
                // remove 1 char at i or j
                // then check for condition
                // if false -> do not need to check the remain positions
                // because it can not be palindrome anymore
                // return -1
                if (CheckPalindrome(s, i + 1, j)) return i; //remove at position i & check
                if (CheckPalindrome(s, i, j - 1)) return j; //remove at position j & check
                return -1; // there is no solution
            }
        }
        return -1; // it's palindrome already  
    }
    public static bool CheckPalindrome(string s, int left, int right)
    {
        for (int i = left, j = right; i < j; i++, j--)
        {
            if (s[i] != s[j]) return false;
        }
        return true;
    
    }
    
  • + 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
    }