Skip to content

Commit 9e04e6c

Browse files
committed
2129. Capitalize the Title: AC
1 parent f1df55d commit 9e04e6c

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,3 +1607,4 @@ mod s2124_check_if_all_as_appears_before_all_bs;
16071607
mod s2125_number_of_laser_beams_in_a_bank;
16081608
mod s2126_destroying_asteroids;
16091609
mod s2127_maximum_employees_to_be_invited_to_a_meeting;
1610+
mod s2129_capitalize_the_title;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* [2129] Capitalize the Title
3+
*
4+
* 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:
5+
*
6+
* If the length of the word is 1 or 2 letters, change all letters to lowercase.
7+
* Otherwise, change the first letter to uppercase and the remaining letters to lowercase.
8+
*
9+
* Return the capitalized title.
10+
*
11+
* Example 1:
12+
*
13+
* Input: title = "capiTalIze tHe titLe"
14+
* Output: "Capitalize The Title"
15+
* Explanation:
16+
* 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.
17+
*
18+
* Example 2:
19+
*
20+
* Input: title = "First leTTeR of EACH Word"
21+
* Output: "First Letter of Each Word"
22+
* Explanation:
23+
* The word "of" has length 2, so it is all lowercase.
24+
* 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.
25+
*
26+
* Example 3:
27+
*
28+
* Input: title = "i lOve leetcode"
29+
* Output: "i Love Leetcode"
30+
* Explanation:
31+
* The word "i" has length 1, so it is lowercase.
32+
* 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.
33+
*
34+
*
35+
* Constraints:
36+
*
37+
* 1 <= title.length <= 100
38+
* title consists of words separated by a single space without any leading or trailing spaces.
39+
* Each word consists of uppercase and lowercase English letters and is non-empty.
40+
*
41+
*/
42+
pub struct Solution {}
43+
44+
// problem: https://leetcode.com/problems/capitalize-the-title/
45+
// discuss: https://leetcode.com/problems/capitalize-the-title/discuss/?currentPage=1&orderBy=most_votes&query=
46+
47+
// submission codes start here
48+
49+
impl Solution {
50+
pub fn capitalize_title(title: String) -> String {
51+
title
52+
.split(" ")
53+
.map(|s| {
54+
s.chars()
55+
.zip(0..)
56+
.map(|(c, i)| match i == 0 && s.len() > 2 {
57+
true => c.to_ascii_uppercase(),
58+
false => c.to_ascii_lowercase(),
59+
})
60+
.collect::<String>()
61+
})
62+
.collect::<Vec<String>>()
63+
.join(" ")
64+
}
65+
}
66+
67+
// submission codes end
68+
69+
#[cfg(test)]
70+
mod tests {
71+
use super::*;
72+
73+
#[test]
74+
fn test_2129_example_1() {
75+
let title = "capiTalIze tHe titLe".to_string();
76+
let result = "Capitalize The Title".to_string();
77+
78+
assert_eq!(Solution::capitalize_title(title), result);
79+
}
80+
81+
#[test]
82+
fn test_2129_example_2() {
83+
let title = "First leTTeR of EACH Word".to_string();
84+
let result = "First Letter of Each Word".to_string();
85+
86+
assert_eq!(Solution::capitalize_title(title), result);
87+
}
88+
89+
#[test]
90+
fn test_2129_example_3() {
91+
let title = "i lOve leetcode".to_string();
92+
let result = "i Love Leetcode".to_string();
93+
94+
assert_eq!(Solution::capitalize_title(title), result);
95+
}
96+
}

0 commit comments

Comments
 (0)