Given a sentence&nbsp;text (A&nbsp;sentence&nbsp;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&nbsp;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&nbsp;following the format shown above.

&nbsp;
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"


&nbsp;
Constraints:


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


class Solution:
    def arrangeWords(self, text: str) -> str:
        l=list(text.split(" "))
        l=sorted(l,key= lambda word: len(word))
        l=' '.join(l)
        return l.capitalize()
class Solution {
    public String arrangeWords(String text) {
        String[] words = text.split(" ");
        for (int i = 0; i < words.length; i++) {
            words[i] = words[i].toLowerCase();
        }
        Arrays.sort(words, (s, t) -> s.length() - t.length());
        words[0] = Character.toUpperCase(words[0].charAt(0)) + words[0].substring(1);
        return String.join(" ", words);
    }
}
class Solution {
public:
    vector<pair<string,int>> words ;
    string arrangeWords(string text) {
        //convert to lowercase alphabet 
        text[0] += 32 ;
        
        istringstream iss(text) ;
        string word = "" ;
		
		//pos is the index of each word in text.
        int pos = 0 ;
        
        while(iss >> word){
            words.push_back({word,pos});
            ++pos ;
        }
        
		//sort by length and pos.
        sort(begin(words),end(words),[&](const pair<string,int> &p1 , const pair<string,int> &p2)->bool{
            if(size(p1.first) == size(p2.first)) return p1.second < p2.second ;
            return size(p1.first) < size(p2.first);
        });
        
        string ans = "" ;
        for(auto &x : words) ans += x.first + " " ;
        ans.pop_back() ;
        
        //convert to uppercase alphabet 
        ans[0] -= 32 ;
        return ans ;
        
        
    }
};
var arrangeWords = function(text) {
    let sorted = text.toLowerCase().split(' ');
    
    sorted.sort((a, b) => a.length - b.length);
    
    sorted[0] = sorted[0].charAt(0).toUpperCase() + sorted[0].slice(1);
    
    return sorted.join(' ');
};
Rearrange Words in a Sentence