Detect Capital Problem


Description

LeetCode Problem 520.

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, like “Google”.

Given a string word, return true if the usage of capitals in it is right.

Example 1:

1
2
Input: word = "USA"
Output: true

Example 2:

1
2
Input: word = "FlaG"
Output: false

Constraints:

  • 1 <= word.length <= 100
  • word consists of lowercase and uppercase English letters.


Sample C++ Code

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
    bool detectCapitalUse(string word) {
        for(int i = 1; i < word.size(); i++) {
            if(isupper(word[1]) != isupper(word[i]) || 
               islower(word[0]) && isupper(word[i])) 
                return false;
        }        
        return true;
    }
};




Related Posts

Validate IP Address Problem

LeetCode 468. Given a string queryIP, return “IPv4” if IP...

Strong Password Checker Problem

LeetCode 420. A password is considered strong if the below...

Find The Closest Palindrome Problem

LeetCode 564. Given a string n representing an integer, return...

Student Attendance Record I Problem

LeetCode 551. You are given a string s representing an...

Repeated Substring Pattern Problem

LeetCode 459. Given a string s, check if it can...

Remove K Digits Problem

LeetCode 402. Given string num representing a non-negative integer num,...