-1

I have made the following code for the above program. In this program the test case "race a car" is not passing i.e. not showing the correct output for this test case. I don't know why. What's the reason? also if there is any way to correct this.

test case

class Solution {
    public boolean isPalindrome(String s) {
        int f = 0, l = s.length() - 1;
        s = s.toLowerCase();

        while (f <= l) {
            char char1 = s.charAt(f);
            char char2 = s.charAt(l);
            
            if (!(char1 >= 'a' && char1 <= 'z')) {
                f++;
            } else if (!(char2 >= 'a' && char2 <= 'z')) {
                l--;
            } else if (!(Character.isDigit(char1))) {
                f++;
            } else if (!(Character.isDigit(char2))) {
                l--;
            } else if (char1 == char2) {
                f++;
                l--;
            } else
                return false;
        }
        
        return true;
    }
} 
0009laH
  • 1,960
  • 13
  • 27
vhvg vhcc
  • 19
  • 1
  • 5
  • did you try debugging your code? After a quick look I would say it's something with `while(f<=l)` – Coreggon Jul 29 '21 at 14:53
  • The very first `if` will cause the code to ignore any non a-z characters, including ignoring any digits... – luk2302 Jul 29 '21 at 14:56
  • 3
    The problem in your logic is that you cannot separate the check whether a char is not a letter and the check whether the character is not a digit. `if (!(char1 >= 'a' && char1 <= 'z'))` or `if (!(Character.isDigit(char1)))` <- One of those conditions will always be true. If a char falls in the a-z range then he is not a digit, and if it is a digit it will not be in the a-z range. Therefor your loop will always just skip and do `f++` until it skipped all characters. You need to do both of those checks in one if condition, aka check if a character is neither a letter nor a digit. – OH GOD SPIDERS Jul 29 '21 at 14:57
  • Please note that this site is not a debugging service. Copy-pasting slabs of code into the question and asking us to debug it is off-topic. However, we can help you with *specific* questions about small *parts* of your code (a few lines), ideally accompanied by an [MCVE](https://stackoverflow.com/help/mcve). Please edit your question to narrow its focus and remove all code not directly relevant. That said, one approach is to delete all your code and replace with `s = s.replaceAll("[^a-zA-Z]", "").toLowerCase(); return s.equals(new StringBuilder(s).reverse().toString());` – Bohemian Jul 29 '21 at 15:30
  • Why would not you delete all non-alphanumeric characters (as @Bohemian suggested) and check if the remaining string is a palindrome (e.g. `for (int i = 0, n = s.length(), j = n - 1; i < n/2; i++. j--) { if (s.charAt(i) != s.charAt(j)) return false;} return true; `)? – Nowhere Man Jul 29 '21 at 15:36

2 Answers2

1

As "OH GOD SPIDERS" has pointed out in the comments, you have a subtle logical flaw in the checks you're performing for non-alphanumeric characters.

Your code is effectively doing:

if(!(char1>='a' && char1<='z') || !Character.isDigit(char1)) 
{
  f++;
}

A whitespace character or digit will match the 1st condition (not a letter) while a letter will match the 2nd condition (not a digit), so the combined expression will always be true.

You instead want to and the two negative conditions:

if(!(char1>='a' && char1<='z') && !Character.isDigit(char1))
{
    f++;
}

or equivalently (De Morgan's Law):

if(!((char1>='a' && char1<='z') || Character.isDigit(char1) ))
{
    f++;
}

This will only be true of whitespace or non-alphanumeric characters.

RaffleBuffle
  • 5,396
  • 1
  • 9
  • 16
0

Here's a solution in ruby

def is_palindrome(string)
    string = removeNonAlphanumeric(string).gsub(" ","").downcase
    string == string.reverse ? true : false
end

def removeNonAlphanumeric(string)
  string.each_line do |ch|
    ascii = ch.ord
    if !(48 <= ascii && ascii <=57) && !(97 <= ascii && ascii <=122)
      string = string.gsub(ch, "")
    end
  end

  string
end

puts is_palindrome("Eye: eye eYe")