Valid Palindrome Runtime Error


  • 0
    M

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

    For example,
    "A man, a plan, a canal: Panama" is a palindrome.
    "race a car" is not a palindrome.

    Note:
    Have you consider that the string might be empty? This is a good question to ask during an interview.

    For the purpose of this problem, we define empty string as valid palindrome.

    Why is there a runtime error?

    public class Solution {
    public static boolean isPalindrome(String s)
    {
    boolean result = false;

         if (s == "" || s == null)
         {
             result = true;
         }
         else
         {
             String lowerS = s.toLowerCase();
             char[] _lowerS = lowerS.toCharArray();
             int len = _lowerS.length;
             int i = 0;
             int j = len - 1;
             while (i<j&&i<=len-1&&j>=0)
             {
                 while ((_lowerS[i]>='a'&&_lowerS[i]<='z') == false)
                     i++;
                 while ((_lowerS[j]>='a'&&_lowerS[j]<='z') == false)
                     j--;
                 if ((_lowerS[i]== _lowerS[j])==false)
                 {
                     result = false;
                     break;
                 }
                 i++;
                 j--;
             }
             if (i == j+1 ||i ==j+2)
             {
                 result = true;
             }
         }
         return result;
     }
    

    }


Log in to reply
 

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