Leetcode 520: Detect Capital

grid47
grid47
Exploring patterns and algorithms
Sep 16, 2024 4 min read

A word where the capital letters are detected, glowing softly as the word is analyzed.
Solution to LeetCode 520: Detect Capital Problem

We define the correct usage of capital letters in a word as either all letters being capitalized, all letters being lowercase, or only the first letter being capitalized while the rest are lowercase. Given a string word, return true if the word follows one of these patterns, and false otherwise.
Problem
Approach
Steps
Complexity
Input: The input consists of a string `word` which is composed of English alphabet letters.
Example: word = 'HELLO'
Constraints:
• 1 <= word.length <= 100
• The word contains only uppercase and lowercase English letters.
Output: Return true if the capital usage in the word is correct, otherwise return false.
Example: true, false
Constraints:
• The output should be a boolean indicating whether the word follows the correct capitalization rules.
Goal: Check if the capitalization pattern of the word matches one of the valid patterns.
Steps:
• 1. Check if the word is completely uppercase.
• 2. Check if the word is completely lowercase.
• 3. Check if only the first letter is uppercase and the rest are lowercase.
Goal: The constraints for input and output of the problem.
Steps:
• 1 <= word.length <= 100
• The word contains only lowercase and uppercase English letters.
Assumptions:
• The word contains only English alphabet characters.
• The length of the word is between 1 and 100 characters.
Input: word = 'HELLO'
Explanation: The word is all in uppercase, which is valid.

Input: word = 'FlaG'
Explanation: The word has an incorrect capitalization pattern (only the first letter is uppercase and the rest are lowercase), which is invalid.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus