# 2129. Capitalize the Title

• Time: $O(n)$
• Space: $O(n)$
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Solution { public: string capitalizeTitle(string title) { transform(begin(title), end(title), begin(title), ::tolower); int i = 0; // Point to the start of a word int j = 0; // Point to the end of a word while (j < title.length()) { while (j < title.length() && title[j] != ' ') ++j; if (j - i > 2) title[i] = toupper(title[i]); i = j + 1; ++j; // Skip the space } return title; } }; 
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Solution { public String capitalizeTitle(String title) { StringBuilder sb = new StringBuilder(title.toLowerCase()); int i = 0; // Point to the start of a word int j = 0; // Point to the end of a word while (j < sb.length()) { while (j < sb.length() && sb.charAt(j) != ' ') ++j; if (j - i > 2) sb.setCharAt(i, Character.toUpperCase(sb.charAt(i))); i = j + 1; ++j; // Skip the space } return sb.toString(); } } 
 1 2 3 class Solution: def capitalizeTitle(self, title: str) -> str: return ' '.join(s.lower() if len(s) < 3 else s.capitalize() for s in title.split())