2023-01-02 Daily Challenge

Today I have done leetcode's January LeetCoding Challenge with cpp.

January LeetCoding Challenge 2

Description

Detect Capital

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:

Input: word = "USA"
Output: true

Example 2:

Input: word = "FlaG"
Output: false

 

Constraints:

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

Solution

class Solution {
public:
  bool detectCapitalUse(string word) {
    int count = 0;
    for(auto c : word) {
      count += !!isupper(c);
    }
    // int count = count_if(word.begin(), word.end(), isupper);
    return count == word.length() || count == 0 || (count == 1 && isupper(word.front()));
  }
};

// Accepted
// 550/550 cases passed (0 ms)
// Your runtime beats 100 % of cpp submissions
// Your memory usage beats 67.65 % of cpp submissions (6 MB)