Leetcode 520. Detect Capital
Problem
Let's say you are given a string which is a word consisting of uppercase and lowercase English letters. Your task is to write a function, detectCapitalUse
, to check if the usage of capitals in this word is "correct" based on the following specifications:
- All characters in the word are uppercase letters. Example:
USA
- All characters in the word are lowercase letters. Example:
leetcode
- Only the first character in the word is uppercase. Example:
Google
If the word does not fit any of the above three categories, we will consider the usage of capitals as incorrect.
For example,
1 2python 3Input: "USA" 4Output: True 5 6Input: "FlaG" 7Output: False
In the first example, all letters are uppercase which fits one of the correct categories. Hence, it returns True
. In the second example, the word does not fit any of the categories. Hence, it returns False
.
Approach
To solve this problem, you need to iterate through each character in the word beginning from the second character. For each character, you check two conditions:
- If the case (uppercase or lowercase) of the current character is not same as the second character in the word.
- If the first character is lowercase but the current is uppercase.
If any of the condition is True
, return False
immediately. If you have iterated through all the characters in the word and none of the conditions were ever True
, return True
. This approach works because it is essentially the code form of our 3 categories of "correct" capital usage.
Python Solution
1 2python 3class Solution: 4 def detectCapitalUse(self, word: str) -> bool: 5 for i in range(1, len(word)): 6 # check for mismatch in case or if first char is lowercase and current is uppercase 7 if (word[1].isupper() != word[i].isupper()) or (word[0].islower() and word[i].isupper()): 8 return False 9 return True
Java Solution
1
2java
3public class Solution {
4 public boolean detectCapitalUse(String word) {
5 for (int i = 1; i < word.length(); i++)
6 if ((Character.isUpperCase(word.charAt(1)) != Character.isUpperCase(word.charAt(i))) ||
7 (Character.isLowerCase(word.charAt(0)) && Character.isUpperCase(word.charAt(i))))
8 return false;
9 return true;
10 }
11}
Javascript Solution
1
2javascript
3class Solution {
4 detectCapitalUse(word) {
5 for (let i = 1; i < word.length; ++i)
6 if ((word[1] === word[1].toUpperCase()) !== (word[i] === word[i].toUpperCase()) ||
7 (word[0] === word[0].toLowerCase() && word[i] === word[i].toUpperCase()))
8 return false;
9 return true;
10 }
11}
C++ Solution
1
2cpp
3class Solution {
4public:
5 bool detectCapitalUse(string word) {
6 for (int i = 1; i < word.length(); ++i)
7 if (isupper(word[1]) != isupper(word[i]) || islower(word[0]) && isupper(word[i]))
8 return false;
9 return true;
10 }
11};
C# Solution
1
2csharp
3public class Solution {
4 public bool DetectCapitalUse(string word) {
5 for (int i = 1; i < word.Length; i++)
6 if ((char.IsUpper(word[1]) != char.IsUpper(word[i])) ||
7 (char.IsLower(word[0]) && char.IsUpper(word[i])))
8 return false;
9 return true;
10 }
11}
Ruby Solution
1 2ruby 3class Solution 4 def detect_capital_use(word) 5 for i in 1...word.length 6 # check for case mismatch or if first char is lowercase but current is uppercase 7 if (word[1].upcase? != word[i].upcase?) || (word[0].downcase? && word[i].upcase?) 8 return false 9 end 10 end 11 return true 12 end 13end
Scala Solution
1
2scala
3object Solution {
4 def detectCapitalUse(word: String): Boolean = {
5 for (i <- 1 until word.length)
6 if ((Character.isUpperCase(word.charAt(1)) != Character.isUpperCase(word.charAt(i))) ||
7 (Character.isLowerCase(word.charAt(0)) && Character.isUpperCase(word.charAt(i))))
8 return false
9 true
10 }
11}
Golang Solution
1 2golang 3package main 4 5import ( 6 "unicode" 7) 8 9func DetectCapitalUse(word string) bool { 10 for i := 1; i < len(word); i++ { 11 if (unicode.IsUpper(rune(word[1])) != unicode.IsUpper(rune(word[i]))) || 12 (unicode.IsLower(rune(word[0])) && unicode.IsUpper(rune(word[i]))) { 13 return false 14 } 15 } 16 return true 17}
All these solutions have a linear time complexity. They iterate only once through the string to determine if it adheres to the correct capital use rules. Thus, the time complexity of these solutions is O(n) where n represents the length of the string.
Got a question?ย Ask the Teaching Assistantย anything you don't understand.
Still not clear? Ask in the Forum, ย Discordย orย Submitย the part you don't understand to our editors.