#2129

Capitalize the Title

pupil · 310 · lc easy +21 · verified · 67.8% accepted · 812 likes · top 74%

Description

You are given a string title made up of one or more words separated by single spaces. Apply the following capitalization rules to each word:

- If a word has 1 or 2 letters, convert all letters to lowercase.

- Otherwise, capitalize the first letter and lowercase all remaining letters.

Return the transformed title.

Example 1:

Input: title = "capiTalIze tHe titLe"
Output: "Capitalize The Title"
Explanation:
Since all the words have a length of at least 3, the first letter of each word is uppercase, and the remaining letters are lowercase.

Example 2:

Input: title = "First leTTeR of EACH Word"
Output: "First Letter of Each Word"
Explanation:
The word "of" has length 2, so it is all lowercase.
The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.

Example 3:

Input: title = "i lOve leetcode"
Output: "i Love Leetcode"
Explanation:
The word "i" has length 1, so it is lowercase.
The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.

Code

1
2
3