C# Solution


  • 0
            public bool DetectCapitalUse(string word)
            {
                if (word == null || word == string.Empty)
                    return false;
                else if (word.Length == 1)
                    return true;
    
                bool allowLowerCase = false, allowUpperCase = false;
    
                if (((int)word[0] >= 97 && (int)word[0] <= 122) ||
                    ((int)word[0] >= 65 && (int)word[0] <= 90 && (int)word[1] >= 97 && (int)word[1] <= 122))
                    allowLowerCase = true;
    
                if ((int)word[0] >= 65 && (int)word[0] <= 90 && (int)word[1] >= 65 && (int)word[1] <= 90)
                    allowUpperCase = true;
    
                if ((int)word[0] >= 97 && (int)word[0] <= 122 && (int)word[1] >= 65 && (int)word[1] <= 90)
                    return false;
    
                for (int i = 2; i <= word.Length - 1; i++)
                    if (((int)word[i] >= 97 && (int)word[i] <= 122 && !allowLowerCase) ||
                        ((int)word[i] >= 65 && (int)word[i] <= 90 && !allowUpperCase))
                        return false;
    
                return true;
            }

Log in to reply
 

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