C# soln beats 90% and short using LINQ


  • 0
    V

    Count the Capital letters and play with the number of upper case letters to decide true/false. This way we do the scanning of the string just once and being efficient in terms of RT Complexity

    public class Solution {
        public bool DetectCapitalUse(string word) 
        {
            int countCap = word.Count( c => char.IsUpper(c));
            if(countCap == 0) return true;
            else if(countCap == word.Length) return true;
            else if(countCap == 1) 
            {
                if (char.IsUpper(word[0])) 
                    return true; 
                else 
                    return false;
            }
            else
                return false;
        }
    }
    

Log in to reply
 

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