public class Solution {
    public bool DetectCapitalUse(string word) {
        var length = word.Length;

            if (length > 1)
            {
                int UpCaseCount = 0;
                int LowCaseCount = 0;
                bool firstCapital = false;
                bool firstTime = true;
                foreach (var c in word)
                {
                    if (char.IsUpper(c))
                    {
                        UpCaseCount++;

                        if (firstTime)
                        {
                            firstCapital = true;
                        }
                    }
                    else if (char.IsLower(c))
                    {
                        LowCaseCount++;
                    }
                    firstTime = false;
                }

                if (UpCaseCount == 1 && firstCapital)
                {
                    return true;
                }
                if (UpCaseCount == 0 && LowCaseCount == length)
                {
                    return true;
                }
                if (LowCaseCount == 0 && UpCaseCount == length)
                {
                    return true;
                }

                return false;
            }
            else
            {
                return true;
            }
    }
}

https://leetcode.com/problems/detect-capital/#/description

posted on 2017-04-19 10:57  Sempron2800+  阅读(125)  评论(0编辑  收藏  举报