2 line Java Solution


  • 0
    A
    public boolean isPalindrome(String s) {
        String trimmed = s.trim().replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
        return trimmed.equals(new StringBuilder(trimmed).reverse().toString());
    }
    

    After @StefanPochmann suggestion

    public boolean isPalindrome(String s) {
        String trimmed = s.toLowerCase().replaceAll("[^a-z0-9]", "");
        return trimmed.equals(new StringBuilder(trimmed).reverse().toString());
    }

  • 0

    If you do toLowerCase before the replaceAll, you don't need to remove A-Z. Also, trim is unnecessary, the replaceAll will handle that anyway.


  • 0
    A

    @StefanPochmann Thanks for the suggestion, really appreciated !!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.