# Solution For Detect Capital

Problem Statement:

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 except for the first one are capitals, like “leetcode”.
3. All letters in this word are not capitals, like “Google”.

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

Solution:

Let’s go through the solution step-by-step:

• Check if all letters are in uppercase using the isupper() method.

• If yes, the word follows the first case, so return True.

• Check if all letters are in lowercase using the islower() method.

• If yes, the word follows the third case, so return True.

• Check if the first letter of the word is uppercase and the rest of the letters are lowercase using the istitle() method.

• If yes, the word follows the second case, so return True.

• If none of the above cases is true, return False.

Implementation:

def detectCapitalUse(word: str) -> bool:
if word.isupper() or word.islower():
return True
elif word.istitle():
return True
else:
return False

Time Complexity:

O(n), where n is the length of the input word.

Space Complexity:

O(1), as we are not using any extra space.

## Step by Step Implementation For Detect Capital

```class Solution {
public boolean detectCapitalUse(String word) {
// Check if the first letter is capitalized
boolean firstLetterCapitalized = Character.isUpperCase(word.charAt(0));

// If the first letter is capitalized, check if the rest of the letters are all lowercase
if(firstLetterCapitalized){
for(int i = 1; i < word.length(); i++){
if(Character.isUpperCase(word.charAt(i))){
return false;
}
}
}
// If the first letter is not capitalized, check if the rest of the letters are all uppercase
else{
for(int i = 1; i < word.length(); i++){
if(Character.isLowerCase(word.charAt(i))){
return false;
}
}
}

return true;
}
}```
```def detectCapitalUse(word):

# if the word is all uppercase, return True
if word == word.upper():
return True

# if the word is all lowercase, return True
elif word == word.lower():
return True

# if the word is only capitalized at the first letter, return True
elif word[0] == word[0].upper() and word[1:] == word[1:].lower():
return True

# otherwise, return False
else:
return False```
```/**
* @param {string} word
* @return {boolean}
*/
var detectCapitalUse = function(word) {
// create a variable to keep track of uppercase letters
let uppercase = 0;
// loop through each letter in the word
for (let i = 0; i < word.length; i++) {
// if the letter is uppercase, increment the uppercase variable
if (word[i] === word[i].toUpperCase()) {
uppercase++;
}
}
// if all letters are uppercase or all letters are lowercase, return true
if (uppercase === 0 || uppercase === word.length) {
return true;
}
// if the first letter is uppercase and the rest are lowercase, return true
else if (uppercase === 1 && word[0] === word[0].toUpperCase()) {
return true;
}
// otherwise, return false
else {
return false;
}
};```
```class Solution {
public:
bool detectCapitalUse(string word) {
int count = 0;
for (char c: word) {
if (isupper(c)) {
count++;
}
}
if (count == 1 && isupper(word[0])) {
return true;
}
return count == 0 || count == word.length();
}
};```
```public class Solution {
public bool DetectCapitalUse(string word) {

// Create a count variable to keep track of the number of capital letters
int count = 0;

// Loop through each character in the string
foreach(char c in word) {

// If the character is a capital letter, increment the count
if(Char.IsUpper(c)) {
count++;
}
}

// If the count is equal to the length of the string, all letters are capitalized
if(count == word.Length) {
return true;
}

// If the count is equal to 1 and the first letter is capitalized, all other letters are lowercase
else if(count == 1 && Char.IsUpper(word[0])) {
return true;
}

// If the count is equal to 0, all letters are lowercase
else if(count == 0) {
return true;
}

// Otherwise, the word is not in the correct format
else {
return false;
}
}
}```

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]