Capitalize the Title

easy string

Problem

You are given a string title made of one or more words separated by single spaces. Capitalize it: for each word, if its length is 1 or 2 make it all lowercase; otherwise make its first letter uppercase and the rest lowercase. Return the resulting title.

Inputtitle = "capiTalIze tHe titLe"
Output"Capitalize The Title"
Every word here has length ≥ 3, so each is title-cased.

def capitalize_title(title):
    words = title.split(' ')
    for k in range(len(words)):
        w = words[k].lower()
        if len(w) >= 3:
            w = w[0].upper() + w[1:]
        words[k] = w
    return ' '.join(words)
function capitalizeTitle(title) {
  const words = title.split(' ');
  for (let k = 0; k < words.length; k++) {
    let w = words[k].toLowerCase();
    if (w.length >= 3) {
      w = w[0].toUpperCase() + w.slice(1);
    }
    words[k] = w;
  }
  return words.join(' ');
}
class Solution {
    public String capitalizeTitle(String title) {
        String[] words = title.split(" ");
        for (int k = 0; k < words.length; k++) {
            String w = words[k].toLowerCase();
            if (w.length() >= 3) {
                w = Character.toUpperCase(w.charAt(0)) + w.substring(1);
            }
            words[k] = w;
        }
        return String.join(" ", words);
    }
}
string capitalizeTitle(string title) {
    string out = "";
    int i = 0, n = title.size();
    while (i < n) {
        int j = i;
        while (j < n && title[j] != ' ') j++;
        for (int k = i; k < j; k++) {
            char c = tolower(title[k]);
            if (k == i && j - i >= 3) c = toupper(c);
            out += c;
        }
        if (j < n) out += ' ';
        i = j + 1;
    }
    return out;
}
Time: O(n) Space: O(n)