Find if a string is a valid palindrome


  • 0
    B

    private static boolean isPalindrome(String str){
    if(str==null||str.isEmpty())
    return false;
    int length=str.length();
    if(length<=1){
    return true;
    }
    int startIndex=0,endIndex=length-1;
    boolean isPalindrome=false;
    while(startIndex!=endIndex){
    char startChar=str.charAt(startIndex);
    char endChar=str.charAt(endIndex);
    if(!Character.isLetter(startChar)){
    startIndex++;
    continue;
    }
    if(!Character.isLetter(endChar)){
    endIndex--;
    continue;
    }
    isPalindrome=Character.toLowerCase(startChar)==Character.toLowerCase(endChar);
    if(!isPalindrome)
    break;
    startIndex++;
    endIndex--;
    }
    return isPalindrome;
    }


  • 0
    B

    @Bvnaga This is my basic java solution


Log in to reply
 

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