Given a word, you need to judge whether the usage of capitals in it is right or not.api
We define the usage of capitals in a word to be right when one of the following cases holds:this
Otherwise, we define that this word doesn't use capitals in a right way.spa
Example 1:code
Input: "USA" Output: True
Example 2:blog
Input: "FlaG" Output: False
思路:将单词转换为大写获得up,将单词转换为小写获得low,若word与up或与low相等,则返回true,
不然去掉word的首字母获得last,若last转换为小写后仍与last相等,则返回true,
不然返回false。
public boolean detectCapitalUse(String word) { int len=word.length(); String up = word.toUpperCase(); String low = word.toLowerCase(); if (word.equals(up) || word.equals(low)) return true; String last = word.substring(1, len); if (last.toLowerCase().equals(last)) return true; return false; }