Skip to content

Commit a2147e1

Browse files
committed
Runtime 5 ms (Top 33.4%) | Memory 6.0 MB (Top 52.78%)
1 parent e82af52 commit a2147e1

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
class Solution {
22
public:
3-
string capitalizeTitle(string title) {
4-
5-
int len = title.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 && title[i] != ' ') {
12-
title[i] = tolower(title[i]); // converting the character at ith index to lower case ony by one
13-
++i;
14-
}
15-
16-
// if word is of length greater than 2, then turn the first character of the word to upper case
17-
if(i - firstIndex > 2) {
18-
title[firstIndex] = toupper(title[firstIndex]); // converting the first character of the word to upper case
19-
}
20-
}
21-
22-
return title; // return the final result
23-
}
24-
};
3+
string capitalize(string s){
4+
transform(s.begin(), s.end(), s.begin(), ::tolower);
5+
if(s.length() <= 2){
6+
return s;
7+
}
8+
s[0] = s[0] - 'a' + 'A';
9+
return s;
10+
}
11+
string capitalizeTitle(string title) {
12+
string str = "";
13+
string ans = "";
14+
for(int i = 0; i < title.length(); i++){
15+
if(title[i] != ' '){
16+
str.push_back(title[i]);
17+
}
18+
else{
19+
str = capitalize(str);
20+
ans += str + " ";
21+
str = "";
22+
}
23+
}
24+
str = capitalize(str);
25+
ans += str;
26+
return ans;
27+
}
28+
};

0 commit comments

Comments
 (0)