# C#LeetCode刷题之#520-检测大写字母（Detect Capital）

Given a word, you need to judge whether the usage of capitals in it is right or not.

We define the usage of capitals in a word to be right when one of the following cases holds:

All letters in this word are capitals, like “USA”.
All letters in this word are not capitals, like “leetcode”.
Only the first letter in this word is capital if it has more than one letter, like “Google”.
Otherwise, we define that this word doesn’t use capitals in a right way.

Input: “USA”

Output: True

Input: “FlaG”

Output: False

Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.

```public class Program {

public static void Main(string[] args) {
var word = "isA";

var res = DetectCapitalUse(word);
Console.WriteLine(res);

res = DetectCapitalUse2(word);
Console.WriteLine(res);

}

private static bool DetectCapitalUse(string word) {
//定义结果
var res = string.Empty;
//大写字母用1表示，小写字母用0表示
foreach(var c in word) {
if(c >= 'a' && c <= 'z') res += '0';
else res += '1';
}
//去除前导0为空，表示全小写
//去除前导1为空，表示全大写
//去除后导0为1，表示首字母大写
return res.TrimStart('0') == "" ||
res.TrimStart('1') == "" ||
res.TrimEnd('0') == "1";
}

private static bool DetectCapitalUse2(string word) {
//直接判定
return word == word.ToLower() ||
word == word.ToUpper() ||
(word[0].ToString() == word[0].ToString().ToUpper() &&
word.Substring(1) == word.Substring(1).ToLower());
}

}```

```False
True```

DetectCapitalUse 的时间复杂度为 ，DetectCapitalUse2 的时间复杂度也应当为： ​ ，因为使用了部分运行库，不能简单的认为它的时间复杂度为  。