Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Detect Capital problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Detect Capital problem solution 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.

Leetcode Detect Capital problem solution

Leetcode Detect Capital problem solution in Python.

def detectCapitalUse(self, word: str) -> bool:
    b = [c.isupper() for c in word]
    return all(b) or not any(b) or (b[0] and not any(b[1:]))

Detect Capital problem solution in Java.

class Solution {
    
    public boolean isCapital(char c){
        return c >= 65 && c <= 90;
    }
    
    public boolean detectCapitalUse(String word) {
        int len = word.length();
        int capCount = 0;
        
        for(char c : word.toCharArray()){
            if(isCapital(c))
                capCount++;
        }
        
        boolean allCap = len == capCount;
        boolean firstCap = capCount == 1 && isCapital(word.charAt(0));
        boolean noCap = capCount == 0;
        
        return allCap || firstCap || noCap;
    }
}

Problem solution in C++.

class Solution {
public:
    bool detectCapitalUse(string word) {
        if(word.size() == 1) {return true;}
        bool firstC = isupper(word[0]);
        bool allC = isupper(word[1]);
        if(!firstC && allC) {return false;}
        for(uint32_t i = 2; i < word.size(); i++) {
            if((isupper(word[i]) && !allC) || (islower(word[i]) && allC)) {
                return false;
            }
        }
        return true;
    }
};

Problem solution in C.

bool detectCapitalUse(char * word){
    if(strlen(word) == 1)return true;
    int i, small = 0, big = 0;
    for(i=0; word[i]; i++){
        if(word[i]>='a' && word[i] <='z')small++;
        if(word[i]>='A' && word[i] <='Z')big++;
    }
    if(small == 0 && big != 0)return true;
    else if(big == 0 && small != 0)return true;
    else if(big == 1 && word[0]>='A' && word[0] <='Z')return true;
    else return false;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes