520. Detect Capital

来源:互联网 发布:淘宝卖家参加聚划算 编辑:程序博客网 时间:2024/06/05 20:09

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:

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

Example 1:
Input: “USA”
Output: True
Example 2:
Input: “FlaG”
Output: False

Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
Example 1:
Input: “USA”
Output: True
Example 2:
Input: “FlaG”
Output: False
Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.


题意

判断给定的一个字符串是否符合书写规范
规范的定义如下:
1. 所有的字母都是大写
2. 所有的首字母都是小写
3. 首字母大写


算法思路

只需要把字符串和全部大小写的比较,分隔字符串后再比较即可,难度不大


代码实现

package easy;public class DetectCapital {    public boolean detectCapitalUse(String word) {        return word.equals(                word.toLowerCase()) ||                 word.equals(word.toUpperCase()) ||                 (Character.isUpperCase( word.charAt(0) ) &&                 word.substring(1).equals(word.substring(1).toLowerCase()));                 }}
原创粉丝点击