-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapitalize_the_title.py
47 lines (36 loc) · 1.68 KB
/
capitalize_the_title.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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.
# Example 1:
# Input: title = "capiTalIze tHe titLe"
# Output: "Capitalize The Title"
# Explanation:
# Since all the words have a length of at least 3, the first letter of each word is uppercase, and the remaining letters are lowercase.
# Example 2:
# Input: title = "First leTTeR of EACH Word"
# Output: "First Letter of Each Word"
# Explanation:
# The word "of" has length 2, so it is all lowercase.
# The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.
# Example 3:
# Input: title = "i lOve leetcode"
# Output: "i Love Leetcode"
# Explanation:
# The word "i" has length 1, so it is lowercase.
# The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.
class Solution:
def capitalizeTitle(self, title: str) -> str:
words = title.split(' ')
answer = ''
for i in range(len(words)):
if len(words[i]) > 2:
answer += words[i].capitalize()
else:
answer += words[i].lower()
if i == len(words) - 1:
continue
else:
answer += ' '
return answer