
Problem Statement
In the task at hand, we are given a word, and the objective is to determine whether the usage of capital letters within this word adheres to one of three specified criteria:
- All characters in the word are uppercase, such as in "USA".
- All characters are lowercase, as seen in "vultr".
- Only the initial character of the word is uppercase, illustrated by "Google".
The function should return true
if the word's capitalization pattern matches any of the above conditions, otherwise false
.
Examples
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.
Approach and Intuition
To solve this problem, we can observe that:
- If the word is entirely uppercase, such as "USA", a simple check of the word against its uppercased version will confirm this condition.
- If the word is completely lowercase, like "vultr", comparing the word to its lowercased form will help ascertain this pattern.
- If only the first letter of the word is uppercase, we can verify this by ensuring that the first character of the word when capitalized remains the same while the rest of the word matches its lowercased form.
Let’s break down the solution steps based on the given examples and the defined constraints:
Example 1 ("USA"):
- The word is all uppercase, which matches the first condition.
- As a result, we would return
true
.
Example 2 ("FlaG"):
- Here, the first letter is uppercase but not all the other letters are lowercase. Therefore, this does not precisely match any of the three described conditions.
- Hence, the function should return
false
.
Considering the constraints provided (word length and character types), this problem can be approached efficiently using simple string methods available in most programming languages, allowing for an approach that operates within O(n) time complexity, where n is the length of the string. This ensures that even for the longest possible string of 100 characters, the solution remains computationally feasible.
Solutions
- Java
- Python
class Solution {
public boolean isCapitalUsageValid(String text) {
return text.matches("[A-Z]*|.[a-z]*");
}
}
This Java solution resolves the problem of checking if the capital usage in a given string is valid. Implement the method isCapitalUsageValid
which takes a string text
as input and returns a boolean value. The method employs regular expressions to verify the correctness of capital letter usage in the string. The regular expression [A-Z]*|.[a-z]*
checks two patterns:
[A-Z]*
ensures all characters in the string are uppercase letters..[a-z]*
allows for a string where the first character can be any character, followed by all lowercase letters.
This implementation is efficient for determining if the text adheres to either all characters being uppercase or the first character being unrestricted with all others being lowercase. Use this method directly by passing the string you need to verify, and it will return true
if the string follows one of the allowed patterns, otherwise false
.
import re
class Solution:
def isCapitalUsageCorrect(self, input_word: str) -> bool:
return re.fullmatch(r"[A-Z]*|.[a-z]*", input_word)
The provided Python solution for the 'Detect Capital' problem uses the re
library to determine if the capitalization in the given word is correct. The method isCapitalUsageCorrect
inside the Solution
class takes a single parameter, input_word
, which is a string. It returns True
if the word matches one of the specified patterns for correct capitalization and False
otherwise. The regular expression used, r"[A-Z]*|.[a-z]*"
, checks for two patterns:
- A word made entirely of uppercase letters.
- A word that begins with any character followed by lowercase letters.
This method effectively checks for correct usage of capital letters with minimal code, leveraging Python's powerful regex capabilities for pattern matching.
No comments yet.