Given a sentence represented as a string text
, where words are separated by single spaces and the sentence starts with a capital letter, rearrange the words in the sentence in ascending order by the length of each word. If two words have the same length, maintain their original relative order. The output should be a single string with the following properties:
text
.To solve this problem, the first idea might be to split the sentence into words, sort them by length, and then join them back. However, we also need to handle capitalization: only the first word should be capitalized, and all others should be lowercase at the start. Additionally, we must maintain the original order of words with the same length, which suggests a stable sort is necessary.
The brute-force approach would involve nested loops or manual insertion, but sorting algorithms like sorted()
in Python or Arrays.sort()
in Java can handle stability for us. The main challenge is to carefully process capitalization after sorting and ensure the sentence is reconstructed correctly.
Here is a step-by-step breakdown of the algorithm:
text
into a list of words using space as the delimiter.We use a stable sort because it preserves the original order of words with equal length, as required by the problem. The capitalization step is handled after sorting to avoid losing information about the original casing.
Input: text = "Leetcode is cool"
["Leetcode", "is", "cool"]
["leetcode", "is", "cool"]
["is", "cool", "leetcode"]
["Is", "cool", "leetcode"]
"Is cool leetcode"
The output sentence starts with a capital letter, and the words are sorted by length, with "is" (2), "cool" (4), and "leetcode" (8).
The sorting step dominates the time complexity. The approach is efficient for typical sentence lengths.
The key to solving this problem efficiently is to:
class Solution:
def arrangeWords(self, text: str) -> str:
words = text.split()
words[0] = words[0].lower()
words.sort(key=len)
words[0] = words[0].capitalize()
return ' '.join(words)
class Solution {
public:
string arrangeWords(string text) {
vector<string> words;
istringstream iss(text);
string word;
while (iss >> word) {
words.push_back(word);
}
words[0][0] = tolower(words[0][0]);
stable_sort(words.begin(), words.end(), [](const string& a, const string& b) {
return a.size() < b.size();
});
words[0][0] = toupper(words[0][0]);
string result;
for (int i = 0; i < words.size(); ++i) {
if (i > 0) result += ' ';
result += words[i];
}
return result;
}
};
class Solution {
public String arrangeWords(String text) {
String[] words = text.split(" ");
words[0] = words[0].toLowerCase();
Arrays.sort(words, (a, b) -> Integer.compare(a.length(), b.length()));
words[0] = words[0].substring(0, 1).toUpperCase() + words[0].substring(1);
return String.join(" ", words);
}
}
var arrangeWords = function(text) {
let words = text.split(" ");
words[0] = words[0].toLowerCase();
words.sort((a, b) => a.length - b.length);
words[0] = words[0][0].toUpperCase() + words[0].slice(1);
return words.join(" ");
};