Skip to main content
Back to problems
#2129
Easy Algorithms

Capitalize the title

String
67.8% acceptance
Feb 25, 2026
812
54
You are given a string title consisting of one or more words separated by a single space, where each word consists of English letters. Capitalize the string by changing the capitalization of each word such that: If the length of the word is 1 or 2 letters, change all letters to lowercase. Otherwise, change the first letter to uppercase and the remaining letters to lowercase. Return the capitalized title.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn capitalize_title(title: String) -> String {
    title
      .split_whitespace()
      .map(|word| {
        if word.len() <= 2 {
          word.to_lowercase()
        } else {
          let mut chars = word.chars();
          match chars.next() {
            Some(c) => {
              c.to_uppercase().to_string() + &chars.as_str().to_lowercase()
            }
            None => String::new(),
          }
        }
      })
      .collect::<Vec<_>>()
      .join(" ")
  }
}