Is palindrome valid if string is not alphanumeric at all?

  • 0

    Is palindrome valid if string is not alphanumeric at all?

    Suppose given string is "#S&&$#"
    IMO this is not valid palindrome, it should return false.

    But my solution gives true, which also is accepted here.

    bool isPalindrome(std::string s) {
    if(s.empty() == true)
    return true;
    int left = 0;
    int right = s.size() - 1;
    while(left < right)
    if(is_alphanumeric(s[left]) == false)
    else if(is_alphanumeric(s[right]) == false)
    else if(to_upper(s[left++]) != to_upper(s[right--]))
    return false;
    return true;

    Should I treat this as valid palindrome? If yes, why?

  • 0
    This post is deleted!

  • 0

    For strings which do not contain any alphanumeric chars, they are automatically considered as palindrome in this problem. The problem defined here actually has the following assumptions:

    • Ignoring any non-alphanumeric chars, which means the given string can be pre-processed by removing all such "invalid" chars before checking.
    • Empty string is considered palindrome.

    Therefore, strings like "!@#$%^" are palindrome.

    Btw, you do not need the edge case check if(s.empty() == true) in your code since it is already covered by your implementation.

Log in to reply

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