Skip to content

Commit 6345aaa

Browse files
committed
2129: Capitalize the Title; solution & tests.
1 parent 752d564 commit 6345aaa

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* 2129
3+
* Capitalize the Title
4+
**
5+
* You are given a string title
6+
* consisting of one or more words separated by a single space,
7+
* where each word consists of English letters.
8+
* Capitalize the string by changing the capitalization of each word such that:
9+
* • If the length of the word is 1 or 2 letters, change all letters to lowercase.
10+
* • Otherwise, change the first letter to uppercase and the remaining letters to lowercase.
11+
*
12+
* Return the capitalized title.
13+
*
14+
* Example 1:
15+
* Input: title = "capiTalIze tHe titLe"
16+
* Output: "Capitalize The Title"
17+
* Explanation:
18+
* Since all the words have a length of at least 3,
19+
* the first letter of each word is uppercase,
20+
* and the remaining letters are lowercase.
21+
*
22+
* Example 2:
23+
* Input: title = "First leTTeR of EACH Word"
24+
* Output: "First Letter of Each Word"
25+
* Explanation:
26+
* The word "of" has length 2, so it is all lowercase.
27+
* The remaining words have a length of at least 3,
28+
* so the first letter of each remaining word is uppercase,
29+
* and the remaining letters are lowercase.
30+
*
31+
* Example 3:
32+
* Input: title = "i lOve leetcode"
33+
* Output: "i Love Leetcode"
34+
* Explanation:
35+
* The word "i" has length 1, so it is lowercase.
36+
* The remaining words have a length of at least 3,
37+
* so the first letter of each remaining word is uppercase,
38+
* and the remaining letters are lowercase.
39+
*
40+
* Constraints:
41+
* • 1 <= title.length <= 100
42+
* • title consists of words separated by a single space without any leading or trailing spaces.
43+
* • Each word consists of uppercase and lowercase English letters and is non-empty.
44+
*
45+
* Hint 1:
46+
* Firstly, try to find all the words present in the string.
47+
*
48+
* Hint 2:
49+
* On the basis of each word's lengths,
50+
* simulate the process explained in Problem.
51+
**
52+
* https://leetcode.com/problems/capitalize-the-title/
53+
***/
54+
55+
using System;
56+
57+
namespace Problems;
58+
59+
public class CapitalizeTheTitle
60+
{
61+
public string CapitalizeTitle( string title )
62+
{
63+
char[] result = new char[title.Length];
64+
int left = 0;
65+
66+
for ( int right = 0; right < result.Length; right++ )
67+
{
68+
if ( title[right] == ' ' )
69+
{
70+
result[right] = title[right];
71+
72+
if ( right - left > 2 )
73+
{
74+
result[left] = Char.ToUpper( title[left] );
75+
}
76+
77+
left = right + 1;
78+
}
79+
else
80+
{
81+
result[right] = Char.ToLower( title[right] );
82+
}
83+
}
84+
85+
if ( result.Length - left > 2 )
86+
{
87+
result[left] = Char.ToUpper( title[left] );
88+
}
89+
90+
return new String( result );
91+
}
92+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using NUnit.Framework;
2+
3+
using Problems;
4+
5+
public class CapitalizeTheTitleTests
6+
{
7+
[TestCase( "capiTalIze tHe titLe", ExpectedResult = "Capitalize The Title" )]
8+
[TestCase( "First leTTeR of EACH Word", ExpectedResult = "First Letter of Each Word" )]
9+
[TestCase( "i lOve leetcode", ExpectedResult = "i Love Leetcode" )]
10+
[TestCase( "L hV", ExpectedResult = "l hv" )]
11+
[TestCase( "ZW Cl pyR uoC", ExpectedResult = "zw cl Pyr Uoc" )]
12+
public string CapitalizeTitleTest( string title ) =>
13+
new CapitalizeTheTitle().CapitalizeTitle( title );
14+
}

0 commit comments

Comments
 (0)