-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
32 lines (28 loc) · 1.04 KB
/
Solution.cs
File metadata and controls
32 lines (28 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
namespace LeetCode.Problem2129{
//2129. Capitalize the Title
//https://leetcode.com/problems/capitalize-the-title/
/*
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:
If the length of the word is 1 or 2 letters, change all letters to lowercase.
Otherwise, change the first letter to uppercase and the remaining letters to lowercase.
Return the capitalized title.
*/
public class Solution {
public string CapitalizeTitle(string title) {
var lower = title
.ToLower()
.Split(' ')
.Select(p=>p.ToCharArray())
.ToList();
string ans = string.Empty;
foreach (var item in lower)
{
if (item.Length > 2)
item[0] = char.ToUpper(item[0]);
ans += new string(item)+" ";
}
ans = ans.TrimEnd(' ');
return ans;
}
}
}