diff --git a/solution/2100-2199/2129.Capitalize the Title/README.md b/solution/2100-2199/2129.Capitalize the Title/README.md index 11d777781f25c..d51e00f9264f9 100644 --- a/solution/2100-2199/2129.Capitalize the Title/README.md +++ b/solution/2100-2199/2129.Capitalize the Title/README.md @@ -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 += " "; } @@ -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(' '); -} -``` - - - -### 方法二 - - - ```ts function capitalizeTitle(title: string): string { return title @@ -152,6 +134,22 @@ function capitalizeTitle(title: string): string { } ``` +```cs +public class Solution { + public string CapitalizeTitle(string title) { + List ans = new List(); + 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); + } +} +``` + diff --git a/solution/2100-2199/2129.Capitalize the Title/README_EN.md b/solution/2100-2199/2129.Capitalize the Title/README_EN.md index 751264628015b..aa51cd479b7bd 100644 --- a/solution/2100-2199/2129.Capitalize the Title/README_EN.md +++ b/solution/2100-2199/2129.Capitalize the Title/README_EN.md @@ -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`. @@ -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 += " "; } @@ -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(' '); -} -``` - - - -### Solution 2 - - - ```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 ans = new List(); + 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); + } +} +``` + diff --git a/solution/2100-2199/2129.Capitalize the Title/Solution.cpp b/solution/2100-2199/2129.Capitalize the Title/Solution.cpp index ca296ef84601d..23b0d2aef24de 100644 --- a/solution/2100-2199/2129.Capitalize the Title/Solution.cpp +++ b/solution/2100-2199/2129.Capitalize the Title/Solution.cpp @@ -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 += " "; } diff --git a/solution/2100-2199/2129.Capitalize the Title/Solution.cs b/solution/2100-2199/2129.Capitalize the Title/Solution.cs new file mode 100644 index 0000000000000..1017e49a5d0e2 --- /dev/null +++ b/solution/2100-2199/2129.Capitalize the Title/Solution.cs @@ -0,0 +1,13 @@ +public class Solution { + public string CapitalizeTitle(string title) { + List ans = new List(); + 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); + } +} \ No newline at end of file diff --git a/solution/2100-2199/2129.Capitalize the Title/Solution.ts b/solution/2100-2199/2129.Capitalize the Title/Solution.ts index aa7383d6f0cb6..dfb551cf6b2aa 100644 --- a/solution/2100-2199/2129.Capitalize the Title/Solution.ts +++ b/solution/2100-2199/2129.Capitalize the Title/Solution.ts @@ -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(' '); } diff --git a/solution/2100-2199/2129.Capitalize the Title/Solution2.ts b/solution/2100-2199/2129.Capitalize the Title/Solution2.ts deleted file mode 100644 index a094c14d20ba0..0000000000000 --- a/solution/2100-2199/2129.Capitalize the Title/Solution2.ts +++ /dev/null @@ -1,10 +0,0 @@ -function capitalizeTitle(title: string): string { - return title - .split(' ') - .map(s => - s.length < 3 - ? s.toLowerCase() - : s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase(), - ) - .join(' '); -}