C# Solution


  • 1
    K
    public class Solution {
        public bool IsPalindrome(string s)
        {
            int left = 0;
            int right = s.Length - 1;
            
            while (left < right){
                bool isLeft = IsAlphaNumeric(s[left]);
                bool isRight = IsAlphaNumeric(s[right]);
                
                if (isLeft && isRight){
                    if (Char.ToLower(s[left]) != Char.ToLower(s[right]))
                        return false;
                    right--;
                    left++;
                }
                if (!isRight){
                    right--;
                }
                if (!isLeft){
                    left++;
                }
            }
            
            return true;
        }
        
        private bool IsAlphaNumeric(char c){
            if (c - '0' >= 0 && c - '0' <= 9)
                return true;
            if (c - 'a' >= 0 && c - 'a' <= 25)
                return true;
            if (c - 'A' >= 0 && c - 'A' <= 25)
                return true;
            return false;
        }
    }

Log in to reply
 

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