C# Solution


  • 0
    public class Solution {
        public bool IsPalindrome(string s)
        {
            s = s.ToLowerInvariant();
            for (int i = 0, j = s.Length - 1; i < s.Length && j >= 0; i++, j--)
            {
                while (i < s.Length && !IsAlphaNumeric(s[i]))
                    i++;
    
                while (j >= 0 && !IsAlphaNumeric(s[j]))
                    j--;
    
                if (i >= s.Length || j < 0)
                    continue;
                    
                if (s[i] != s[j])
                    return false;
            }
    
            return true;
        }
    
        private bool IsAlphaNumeric(char c)
        {
            return c != ' ' &&
                  (c >= 'a' && c <= 'z') ||
                  (c >= 'A' && c <= 'Z') ||
                  (c >= '0' && c <= '9');
        }
    }
    

Log in to reply
 

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