Skip to content

feat: add solutions to lc problem: No.2129 #2431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions solution/2100-2199/2129.Capitalize the Title/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ public:
istringstream ss(title);
string ans;
while (ss >> title) {
if (title.size() > 2) title[0] = toupper(title[0]);
if (title.size() > 2) {
title[0] = toupper(title[0]);
}
ans += title;
ans += " ";
}
Expand All @@ -119,26 +121,6 @@ func capitalizeTitle(title string) string {
}
```

```ts
function capitalizeTitle(title: string): string {
const ans: string[] = [];
for (const s of title.split(' ')) {
if (s.length < 3) {
ans.push(s.toLowerCase());
} else {
ans.push(s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase());
}
}
return ans.join(' ');
}
```

<!-- tabs:end -->

### 方法二

<!-- tabs:start -->

```ts
function capitalizeTitle(title: string): string {
return title
Expand All @@ -152,6 +134,22 @@ function capitalizeTitle(title: string): string {
}
```

```cs
public class Solution {
public string CapitalizeTitle(string title) {
List<string> ans = new List<string>();
foreach (string s in title.Split(' ')) {
if (s.Length < 3) {
ans.Add(s.ToLower());
} else {
ans.Add(char.ToUpper(s[0]) + s.Substring(1).ToLower());
}
}
return string.Join(" ", ans);
}
}
```

<!-- tabs:end -->

<!-- end -->
50 changes: 25 additions & 25 deletions solution/2100-2199/2129.Capitalize the Title/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ The remaining words have a length of at least 3, so the first letter of each rem

## Solutions

### Solution 1
### Solution 1: Simulation

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.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string `title`.

<!-- tabs:start -->

Expand Down Expand Up @@ -91,7 +95,9 @@ public:
istringstream ss(title);
string ans;
while (ss >> title) {
if (title.size() > 2) title[0] = toupper(title[0]);
if (title.size() > 2) {
title[0] = toupper(title[0]);
}
ans += title;
ans += " ";
}
Expand All @@ -114,39 +120,33 @@ func capitalizeTitle(title string) string {
}
```

```ts
function capitalizeTitle(title: string): string {
const ans: string[] = [];
for (const s of title.split(' ')) {
if (s.length < 3) {
ans.push(s.toLowerCase());
} else {
ans.push(s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase());
}
}
return ans.join(' ');
}
```

<!-- tabs:end -->

### Solution 2

<!-- tabs:start -->

```ts
function capitalizeTitle(title: string): string {
return title
.split(' ')
.map(s =>
s.length < 3
? s.toLowerCase()
: s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase(),
s.length < 3 ? s.toLowerCase() : s.slice(0, 1).toUpperCase() + s.slice(1).toLowerCase(),
)
.join(' ');
}
```

```cs
public class Solution {
public string CapitalizeTitle(string title) {
List<string> ans = new List<string>();
foreach (string s in title.Split(' ')) {
if (s.Length < 3) {
ans.Add(s.ToLower());
} else {
ans.Add(char.ToUpper(s[0]) + s.Substring(1).ToLower());
}
}
return string.Join(" ", ans);
}
}
```

<!-- tabs:end -->

<!-- end -->
4 changes: 3 additions & 1 deletion solution/2100-2199/2129.Capitalize the Title/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ class Solution {
istringstream ss(title);
string ans;
while (ss >> title) {
if (title.size() > 2) title[0] = toupper(title[0]);
if (title.size() > 2) {
title[0] = toupper(title[0]);
}
ans += title;
ans += " ";
}
Expand Down
13 changes: 13 additions & 0 deletions solution/2100-2199/2129.Capitalize the Title/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Solution {
public string CapitalizeTitle(string title) {
List<string> ans = new List<string>();
foreach (string s in title.Split(' ')) {
if (s.Length < 3) {
ans.Add(s.ToLower());
} else {
ans.Add(char.ToUpper(s[0]) + s.Substring(1).ToLower());
}
}
return string.Join(" ", ans);
}
}
15 changes: 6 additions & 9 deletions solution/2100-2199/2129.Capitalize the Title/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
function capitalizeTitle(title: string): string {
const ans: string[] = [];
for (const s of title.split(' ')) {
if (s.length < 3) {
ans.push(s.toLowerCase());
} else {
ans.push(s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase());
}
}
return ans.join(' ');
return title
.split(' ')
.map(s =>
s.length < 3 ? s.toLowerCase() : s.slice(0, 1).toUpperCase() + s.slice(1).toLowerCase(),
)
.join(' ');
}
10 changes: 0 additions & 10 deletions solution/2100-2199/2129.Capitalize the Title/Solution2.ts

This file was deleted.