Check if string is palindrome LeetCode LeetCode 125 requires receiving a string and checking if it is a valid palindrome. I have done this and am confident with an int and a single word, but this question requires the use a of a sentence with non-alphanumeric characters. The function must ignore non-alphanumeric characters and whitespaces.
public bool isPalindrome(string s) {

    s = System.Text.RegularExpressions.Regex.Replace(s, @&quot;\W|_&quot;, &quot;&quot;);

    if (s == null || s == &quot;&quot; || s.Length == 1 || s == &quot; &quot;)
    {
        return true;
    }

    for (int i = 0, j = s.Length - 1; i &lt;= s.Length / 2  &amp;&amp; j &gt;= s.Length /2;i--,j++)
    {
        if (Char.ToLower(s[i]) == Char.ToLower(s[j]))
        {
            continue;
        }
        return false;
    }

    return true;
}

It is my first time ever using Regex, maybe that gives my function a performance hit? Would it be better if I could implement this without Regex?
Example inputs are: &quot;a man, a plan, a canal: Panama&quot; -- returns true, &quot;race a car&quot; -- returns false, &quot;a man, a plan, a canal -- Panama&quot; -- returns true. The stats on this are: Runtime 136 ms, faster than 11.75% of c# submissions.
Disclaimer: Not a Code Reviewer

Your solution looks fine;
Overall, we would want to stay away from regular expressions in solving algorithm problems – especially easy LeetCode questions are to measure our implementation skills;
C# has two simple functions that would do the job of (\W|_):

public class Solution {
    public bool IsPalindrome(string s) {
        var left = 0;
        var right = s.Length - 1;

        while (left &lt; right) {
            if (!char.IsLetterOrDigit(s[left])) {
                ++left;

            } else if (!char.IsLetterOrDigit(s[right])) {
                --right;

            } else {
                if (char.ToLower(s[left]) != char.ToLower(s[right])) {
                    return false;
                }

                ++left;
                --right;
            }
        }

        return true;
    }
}



We can just a bit improve the runtime, but not so much, using a more readable expression such as:

(?i)[^a-z0-9]+;

(?i) matches the remainder of the pattern based on the insensitive flag;


[^A-Za-z0-9]+;




public class Solution {

    public bool IsPalindrome(string s) {

        s = System.Text.RegularExpressions.Regex.Replace(s, @&quot;(?i)[^a-z0-9]+&quot;, &quot;&quot;);

        if (s == null || s == &quot;&quot; || s.Length == 1 || s == &quot; &quot;) {
            return true;
        }

        for (int i = 0, j = s.Length - 1; i &lt;= s.Length / 2  &amp;&amp; j &gt;= s.Length / 2; i++, j--) {
            if (Char.ToLower(s[i]) == Char.ToLower(s[j])) {
                continue;
            }

            return false;
        }

        return true;
    }
}

RegEx Circuit
jex.im visualizes regular expressions:


Demo
If you wish to simplify/update/explore the expression, it's been explained on the top right panel of regex101.com. You can watch the matching steps or modify them in this debugger link, if you'd be interested. The debugger demonstrates that how a RegEx engine might step by step consume some sample input strings and would perform the matching process.

Happy Coding! ( •ˆ_ˆ• )