Detect Capital Leetcode Solution

Difficulty Level Easy
Frequently asked in Amazon Google
StringViews 2215

Problem Statement:

Detect Capital Leetcode Solution says that – Given a string, return true if the usage of capitals in it is right. The conditions for the right words are :

  1. All letters in this word are capitals, like "UK".
  2. All letters in this word are not capitals, like "going".
  3. Only the first letter in this word is capital, like "Mango".

 

Example:

Input:  word: “India”

Output: true

Input: word: “HaPpy”

Output: false

Explanation:

Detect Capital Leetcode Solution

This satisfies condition number 1. All the letters are capitalized. So the output will be “TRUE”.

Detect Capital Leetcode Solution

This satisfies condition number 2. All the letters are small. So the output will be “TRUE”.

Detect Capital Leetcode Solution
This satisfies condition number 3. So the output will be “TRUE”.

Detect Capital Leetcode Solution

 This doesn’t satisfy any of the conditions. So the output will be “False”.

 

Approach:

Idea:

First, take two variables to say, “capital” and “small” to store the count of the capital letters and the small letters which are present in the given string. Now iterate through the string and update the values accordingly, which means if a capital letter is found then increase the count of the “capital” variable otherwise increase the “small” variable.

Then check the first two conditions. If the length of the given string is equal to any of the variable “capital” or “small”, return true as all the letters of the word may be capital or may be small.

After that, check for the third given condition where first check if the first character of the string is a capital letter or not and the length of the remaining string is equal to the “small” variable or not. If both of them are true, return true.

Finally, if none of the above conditions returns true, then return false.

 

Code:

C++ code for Detect Capital Leetcode Solution :

class Solution {
public:
    bool detectCapitalUse(string word) {
        int capitals=0, smalls=0;
        for(int i=0;i<word.size();i++){
            if(word[i]>='A' && word[i]<='Z'){
                capitals++;
            }
            else{
                smalls++;
            }
        }
        if(capitals==word.size() || smalls==word.size()){
            return true;
        }
        else if(word[0]>='A' && word[0]<='Z' && smalls==word.size()-1){
            return true;
        }
        else{
            return false;
        }
    }
};

Java code for Detect Capital Leetcode Solution :

class Solution {
    public boolean detectCapitalUse(String word) {
        int capitals=0, smalls=0;
        for(int i=0;i<word.length();i++){
            if(word.charAt(i)>='A' && word.charAt(i)<='Z'){
                capitals++;
            }
            else{
                smalls++;
            }
        }
        if(capitals==word.length() || smalls==word.length()){
            return true;
        }
        else if(word.charAt(0)>='A' && word.charAt(0)<='Z' && smalls==word.length()-1){
            return true;
        }
        else{
            return false;
        }
    }
}

Complexity Analysis for Detect Capital Leetcoee Solution:

Time Complexity:

Time complexity will be O(N) because of iterating over the whole string.

Space Complexity:

Space complexity will be O(1) as no extra space is used.

Reference: https://tutorialcup.com/python/python-string.htm

Translate »