Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Java (with regex), 134 bytes

s->s.matches("|(?:(.)(?<=(?=^.*?(\\1\\2?)$).*))+(?<=(?=^\\2$).*)")//)")*.)$2\\^=?(=<?(+))*.)$)?2\\1\\(?*.^=?(=<?().(:?(|"(sehctam.s>-s

Credits where dueCredits where due

Testing

import java.util.function.*;

class Ideone {
  static Predicate<String> isPalindrome = 
    s->s.matches("|(?:(.)(?<=(?=^.*?(\\1\\2?)$).*))+(?<=(?=^\\2$).*)")//)")*.)$2\\^=?(=<?(+))*.)$)?2\\1\\(?*.^=?(=<?().(:?(|"(sehctam.s>-s
    ;
  
  public static void main (String[] args) throws java.lang.Exception {
    testPalindrome("", true);
    testPalindrome("x", true);
    testPalindrome("xx", true);
    testPalindrome("xy", false);
    testPalindrome("xyx", true);
    testPalindrome("xxx", true);
    testPalindrome("xxyx", false);
    testPalindrome("racecar", true);
    testPalindrome("step on no pets", true);
    testPalindrome("aManaPlanaCanalPanaMa", true);
    testPalindrome("this is impossible", false);
  }
  
  static void testPalindrome(String s, boolean expected) {
    if (isPalindrome.test(s) == expected) {
      System.out.println("OK");
    } else {
      System.out.printf("NOK: str=\"%s\", expected=%b%n", s, expected);
    }
  }
}

Test it yourself!

Java (with regex), 134 bytes

s->s.matches("|(?:(.)(?<=(?=^.*?(\\1\\2?)$).*))+(?<=(?=^\\2$).*)")//)")*.)$2\\^=?(=<?(+))*.)$)?2\\1\\(?*.^=?(=<?().(:?(|"(sehctam.s>-s

Credits where due

Testing

import java.util.function.*;

class Ideone {
  static Predicate<String> isPalindrome = 
    s->s.matches("|(?:(.)(?<=(?=^.*?(\\1\\2?)$).*))+(?<=(?=^\\2$).*)")//)")*.)$2\\^=?(=<?(+))*.)$)?2\\1\\(?*.^=?(=<?().(:?(|"(sehctam.s>-s
    ;
  
  public static void main (String[] args) throws java.lang.Exception {
    testPalindrome("", true);
    testPalindrome("x", true);
    testPalindrome("xx", true);
    testPalindrome("xy", false);
    testPalindrome("xyx", true);
    testPalindrome("xxx", true);
    testPalindrome("xxyx", false);
    testPalindrome("racecar", true);
    testPalindrome("step on no pets", true);
    testPalindrome("aManaPlanaCanalPanaMa", true);
    testPalindrome("this is impossible", false);
  }
  
  static void testPalindrome(String s, boolean expected) {
    if (isPalindrome.test(s) == expected) {
      System.out.println("OK");
    } else {
      System.out.printf("NOK: str=\"%s\", expected=%b%n", s, expected);
    }
  }
}

Test it yourself!

Java (with regex), 134 bytes

s->s.matches("|(?:(.)(?<=(?=^.*?(\\1\\2?)$).*))+(?<=(?=^\\2$).*)")//)")*.)$2\\^=?(=<?(+))*.)$)?2\\1\\(?*.^=?(=<?().(:?(|"(sehctam.s>-s

Credits where due

Testing

import java.util.function.*;

class Ideone {
  static Predicate<String> isPalindrome = 
    s->s.matches("|(?:(.)(?<=(?=^.*?(\\1\\2?)$).*))+(?<=(?=^\\2$).*)")//)")*.)$2\\^=?(=<?(+))*.)$)?2\\1\\(?*.^=?(=<?().(:?(|"(sehctam.s>-s
    ;
  
  public static void main (String[] args) throws java.lang.Exception {
    testPalindrome("", true);
    testPalindrome("x", true);
    testPalindrome("xx", true);
    testPalindrome("xy", false);
    testPalindrome("xyx", true);
    testPalindrome("xxx", true);
    testPalindrome("xxyx", false);
    testPalindrome("racecar", true);
    testPalindrome("step on no pets", true);
    testPalindrome("aManaPlanaCanalPanaMa", true);
    testPalindrome("this is impossible", false);
  }
  
  static void testPalindrome(String s, boolean expected) {
    if (isPalindrome.test(s) == expected) {
      System.out.println("OK");
    } else {
      System.out.printf("NOK: str=\"%s\", expected=%b%n", s, expected);
    }
  }
}

Test it yourself!

Source Link
Olivier Grégoire
  • 14.4k
  • 3
  • 32
  • 56

Java (with regex), 134 bytes

s->s.matches("|(?:(.)(?<=(?=^.*?(\\1\\2?)$).*))+(?<=(?=^\\2$).*)")//)")*.)$2\\^=?(=<?(+))*.)$)?2\\1\\(?*.^=?(=<?().(:?(|"(sehctam.s>-s

Credits where due

Testing

import java.util.function.*;

class Ideone {
  static Predicate<String> isPalindrome = 
    s->s.matches("|(?:(.)(?<=(?=^.*?(\\1\\2?)$).*))+(?<=(?=^\\2$).*)")//)")*.)$2\\^=?(=<?(+))*.)$)?2\\1\\(?*.^=?(=<?().(:?(|"(sehctam.s>-s
    ;
  
  public static void main (String[] args) throws java.lang.Exception {
    testPalindrome("", true);
    testPalindrome("x", true);
    testPalindrome("xx", true);
    testPalindrome("xy", false);
    testPalindrome("xyx", true);
    testPalindrome("xxx", true);
    testPalindrome("xxyx", false);
    testPalindrome("racecar", true);
    testPalindrome("step on no pets", true);
    testPalindrome("aManaPlanaCanalPanaMa", true);
    testPalindrome("this is impossible", false);
  }
  
  static void testPalindrome(String s, boolean expected) {
    if (isPalindrome.test(s) == expected) {
      System.out.println("OK");
    } else {
      System.out.printf("NOK: str=\"%s\", expected=%b%n", s, expected);
    }
  }
}

Test it yourself!