Skip to main content
Back to problems
#520
Easy Algorithms

Detect capital

String
56.5% acceptance
Feb 19, 2026
3578
472
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn detect_capital_use(word: String) -> bool {
    let bytes = word.as_bytes();
    let all_upper = bytes.iter().all(|b| b.is_ascii_uppercase());
    let all_lower = bytes.iter().all(|b| b.is_ascii_lowercase());
    let first_cap = bytes[0].is_ascii_uppercase() && bytes[1..].iter().all(|b| b.is_ascii_lowercase());
    all_upper || all_lower || first_cap
  }
}