Skip to content

Commit 8b89c79

Browse files
authored
feat: add solutions to lc problem: No.2129 (doocs#2431)
No.2129.Capitalize the Title
1 parent 3f28c45 commit 8b89c79

File tree

6 files changed

+66
-66
lines changed

6 files changed

+66
-66
lines changed

solution/2100-2199/2129.Capitalize the Title/README.md

+19-21
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ public:
9696
istringstream ss(title);
9797
string ans;
9898
while (ss >> title) {
99-
if (title.size() > 2) title[0] = toupper(title[0]);
99+
if (title.size() > 2) {
100+
title[0] = toupper(title[0]);
101+
}
100102
ans += title;
101103
ans += " ";
102104
}
@@ -119,26 +121,6 @@ func capitalizeTitle(title string) string {
119121
}
120122
```
121123

122-
```ts
123-
function capitalizeTitle(title: string): string {
124-
const ans: string[] = [];
125-
for (const s of title.split(' ')) {
126-
if (s.length < 3) {
127-
ans.push(s.toLowerCase());
128-
} else {
129-
ans.push(s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase());
130-
}
131-
}
132-
return ans.join(' ');
133-
}
134-
```
135-
136-
<!-- tabs:end -->
137-
138-
### 方法二
139-
140-
<!-- tabs:start -->
141-
142124
```ts
143125
function capitalizeTitle(title: string): string {
144126
return title
@@ -152,6 +134,22 @@ function capitalizeTitle(title: string): string {
152134
}
153135
```
154136

137+
```cs
138+
public class Solution {
139+
public string CapitalizeTitle(string title) {
140+
List<string> ans = new List<string>();
141+
foreach (string s in title.Split(' ')) {
142+
if (s.Length < 3) {
143+
ans.Add(s.ToLower());
144+
} else {
145+
ans.Add(char.ToUpper(s[0]) + s.Substring(1).ToLower());
146+
}
147+
}
148+
return string.Join(" ", ans);
149+
}
150+
}
151+
```
152+
155153
<!-- tabs:end -->
156154

157155
<!-- end -->

solution/2100-2199/2129.Capitalize the Title/README_EN.md

+25-25
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ The remaining words have a length of at least 3, so the first letter of each rem
5656

5757
## Solutions
5858

59-
### Solution 1
59+
### Solution 1: Simulation
60+
61+
Directly simulate the process. Split the string by spaces to get each word, then convert each word to the appropriate case as per the problem statement. Finally, join the words with spaces.
62+
63+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string `title`.
6064

6165
<!-- tabs:start -->
6266

@@ -91,7 +95,9 @@ public:
9195
istringstream ss(title);
9296
string ans;
9397
while (ss >> title) {
94-
if (title.size() > 2) title[0] = toupper(title[0]);
98+
if (title.size() > 2) {
99+
title[0] = toupper(title[0]);
100+
}
95101
ans += title;
96102
ans += " ";
97103
}
@@ -114,39 +120,33 @@ func capitalizeTitle(title string) string {
114120
}
115121
```
116122

117-
```ts
118-
function capitalizeTitle(title: string): string {
119-
const ans: string[] = [];
120-
for (const s of title.split(' ')) {
121-
if (s.length < 3) {
122-
ans.push(s.toLowerCase());
123-
} else {
124-
ans.push(s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase());
125-
}
126-
}
127-
return ans.join(' ');
128-
}
129-
```
130-
131-
<!-- tabs:end -->
132-
133-
### Solution 2
134-
135-
<!-- tabs:start -->
136-
137123
```ts
138124
function capitalizeTitle(title: string): string {
139125
return title
140126
.split(' ')
141127
.map(s =>
142-
s.length < 3
143-
? s.toLowerCase()
144-
: s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase(),
128+
s.length < 3 ? s.toLowerCase() : s.slice(0, 1).toUpperCase() + s.slice(1).toLowerCase(),
145129
)
146130
.join(' ');
147131
}
148132
```
149133

134+
```cs
135+
public class Solution {
136+
public string CapitalizeTitle(string title) {
137+
List<string> ans = new List<string>();
138+
foreach (string s in title.Split(' ')) {
139+
if (s.Length < 3) {
140+
ans.Add(s.ToLower());
141+
} else {
142+
ans.Add(char.ToUpper(s[0]) + s.Substring(1).ToLower());
143+
}
144+
}
145+
return string.Join(" ", ans);
146+
}
147+
}
148+
```
149+
150150
<!-- tabs:end -->
151151

152152
<!-- end -->

solution/2100-2199/2129.Capitalize the Title/Solution.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ class Solution {
55
istringstream ss(title);
66
string ans;
77
while (ss >> title) {
8-
if (title.size() > 2) title[0] = toupper(title[0]);
8+
if (title.size() > 2) {
9+
title[0] = toupper(title[0]);
10+
}
911
ans += title;
1012
ans += " ";
1113
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Solution {
2+
public string CapitalizeTitle(string title) {
3+
List<string> ans = new List<string>();
4+
foreach (string s in title.Split(' ')) {
5+
if (s.Length < 3) {
6+
ans.Add(s.ToLower());
7+
} else {
8+
ans.Add(char.ToUpper(s[0]) + s.Substring(1).ToLower());
9+
}
10+
}
11+
return string.Join(" ", ans);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
function capitalizeTitle(title: string): string {
2-
const ans: string[] = [];
3-
for (const s of title.split(' ')) {
4-
if (s.length < 3) {
5-
ans.push(s.toLowerCase());
6-
} else {
7-
ans.push(s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase());
8-
}
9-
}
10-
return ans.join(' ');
2+
return title
3+
.split(' ')
4+
.map(s =>
5+
s.length < 3 ? s.toLowerCase() : s.slice(0, 1).toUpperCase() + s.slice(1).toLowerCase(),
6+
)
7+
.join(' ');
118
}

solution/2100-2199/2129.Capitalize the Title/Solution2.ts

-10
This file was deleted.

0 commit comments

Comments
 (0)