Given a sentence text (A sentence is a string of space-separated words) in the following format:

First letter is in upper case.
Each word in text are separated by a single space.

Your task is to rearrange the words in text such that all words are rearranged in an increasing order of their lengths. If two words have the same length, arrange them in their original order.
Return the new text following the format shown above.
 
Example 1:
Input: text = "Leetcode is cool"
Output: "Is cool leetcode"
Explanation: There are 3 words, "Leetcode" of length 8, "is" of length 2 and "cool" of length 4.
Output is ordered by length and the new first word starts with capital letter.

Example 2:
Input: text = "Keep calm and code on"
Output: "On and keep calm code"
Explanation: Output is ordered as follows:
"On" 2 letters.
"and" 3 letters.
"keep" 4 letters in case of tie order by position in original text.
"calm" 4 letters.
"code" 4 letters.

Example 3:
Input: text = "To be or not to be"
Output: "To be or to be not"

 
Constraints:

text begins with a capital letter and then contains lowercase letters and single space between words.
1 <= text.length <= 10^5

Difficulty: interview

Starter code:

class Solution:
    def arrangeWords(self, text: str) -> str:
        

Solutions:
class Solution:
    def arrangeWords(self, text: str) -> str:
        p=text.split(' ')
        final=''
        j=sorted(p,key=len)
        temp=' '.join(j)
        if temp[0]>='a' and temp[0]<='z':
            s=temp[0].swapcase()
            final=final+s[0]
        else:
            final=final+temp[0]
        for i in range(1,len(temp)):
            if temp[i]>='A' and temp[i]<='Z':
                s=temp[i].swapcase()
                final=final+s[0]
            else:
                final=final+temp[i]
        return final
        


class Solution:
    def arrangeWords(self, text: str) -> str:
        text = text[0].lower() + text[1:] 
        words=[]
        word=''
        word_len=0
        for char in text:
            if char==' ':
                words.append((word_len, word))
                word=''
                word_len=0
            else:
                word+=char
                word_len+=1
        if (word_len>0):
            words.append((word_len, word))
        words.sort(key=lambda tup: tup[0])
        output=''
        for word in words:
            output+=word[1]+' '
        return output[:-1].capitalize()

from operator import itemgetter

class Solution:
    def arrangeWords(self, text: str) -> str:
        words = [[str.lower(v), k, len(v)] for k,v in enumerate(text.split(' '))]
        words2 = sorted(words, key=itemgetter(2))
        print(words)
        print(words2)
        words3 = [word[0] for word in words2]
        print(words3)
        
        words4 = [str.title(words3[x]) if x == 0 else words3[x] for x in range(len(words3))]
        return ' '.join(words4)

class Solution:
    def arrangeWords(self, text: str) -> str:
        counter = 0
        current_word = ''
        words_map = {}
        for i in range(0, len(text)):
            ch = text[i].lower()
            if ch == ' ' or i == len(text) - 1:
                if i == len(text)-1:
                    counter += 1
                    current_word += ch
                    
                if counter not in words_map:
                    words_map[counter] = []
                    
                words_map[counter].append(current_word)
                
                counter = 0
                current_word = ''
            else:
                counter += 1
                current_word += ch
        
        ordered_keys = list(words_map.keys())
        ordered_keys.sort()
        
        ans = ''
        for i in range(0, len(ordered_keys)):
            if i == 0:
                ans = ' '.join(words_map[ordered_keys[i]])
            else:
                ans += ' ' + ' '.join(words_map[ordered_keys[i]])
                
        ans = ans[0].upper() + ans[1:]
        
        return ans


class K:
    def __init__(self, obj):
        self.obj = obj
    def __lt__(self, other):
        return len(self.obj) < len(other.obj)
class Solution:
    def arrangeWords(self, text: str) -> str:
        l = text.split()
        l[0] = l[0].lower()
        l = sorted(l, key=K)
        
        l[0] = l[0].title()
        return ' '.join(l)

class Solution:
    def arrangeWords(self, text: str) -> str:
        res = []
        res_t = ''
        for idx,word in enumerate(text.split()):
            res.append((len(word),idx, word.lower()))
        res.sort()
        for l,_,word in res:
            if res_t == '':
                res_t += word[0].upper() + word[1:]
            else:
                res_t += ' ' + word
        return res_t

class Solution:
    def arrangeWords(self, text: str) -> str:
        dic = collections.OrderedDict()
        for word in text.split():
            n = len(word)
            dic[n] = dic.get(n, []) + [word.lower()]

        res = []
        for key, val in sorted(dic.items()):
            res += val

        return ' '.join(res).capitalize()

class Solution:
    def arrangeWords(self, text: str) -> str:
        dic = collections.OrderedDict()
        for word in text.split():
            dic[len(word)] = dic.get(len(word), []) + [word.lower()]
        res = []    
        for key, val in sorted(dic.items()):
            res += val
        
        res = [res[0].capitalize()] + res[1:]
        return ' '.join(res)

class Solution:
    def arrangeWords(self, text: str) -> str:
        if not text:
            return text
        
        words = text.lower().split(' ')
        
        words = sorted(words, key = lambda x: len(x))
        words[0] = words[0][0].upper() + words[0][1:]
        
        return ' '.join(words)