Capitalize the Title LeetCode solution in Java

1
class Solution {
2
public String capitalizeTitle(String title) {
3

4
char[] ch = title.toCharArray();
5
int len = ch.length;
6

7
for (int i = 0; i < len; ++i) {
8

9
int firstIndex = i; // store the first index of the word
10

11
while (i < len && ch[i] != ' ') {
12
ch[i] =
13
Character.toLowerCase(
14
ch[i]); // converting the character at ith index to lower case ony by one
15
++i;
16
}
17

18
// if word is of length greater than 2, then turn the first character of the word to upper
19
// case
20
if (i - firstIndex > 2) {
21
ch[firstIndex] =
22
Character.toUpperCase(
23
ch[firstIndex]); // converting the first character of the word to upper case
24
}
25
}
26

27
return String.valueOf(ch); // return the final result by converting the char array into string
28
}
29
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0