C# Solution (Detect Capital)


  • 0
    A
     public bool DetectCapitalUse(string word) {
            char[] charArray = word.ToCharArray();
                bool[] boolArray = new bool[charArray.Length];
                int countUpper = 0;
                int countLower = 0;
                for (int i = 0; i < charArray.Length; i++)
                {
                    int charASCII = (int)charArray[i];
                    if (charASCII >= 65 && charASCII <= 90)
                    {
                        boolArray[i] = true;
                        countUpper++;
                    }
                    else
                    {
                        boolArray[i] = false;
                        countLower++;
                    }            
                }
    
                if (countUpper == charArray.Length && countLower == 0) return true;
                else if (countLower == charArray.Length && countUpper == 0) return true;
                else
                {
                    if (countUpper == 1 && boolArray[0] == true)
                    {
                        for (int i = 1; i < boolArray.Length; i++)
                        {
                            if (boolArray[i] == true) return false;
                        }
                        return true;
                    }                
                    else return false;
                } 
        }
    

Log in to reply
 

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