关键点:
使用稳定排序, 根据单词长度进行排序
""" @File : Demo
@Author : BabyMuu
@Time : 2022/5/13 22:44
"""
class Solution:
def arrangeWords(self, text: str) -> str:
text = text.lower().split()
text[0] = text[0].lower()
text = sorted(text, key=lambda x: len(x))
text[0] = text[0].title()
return " ".join(text)
def one_line(self, text: str) -> str:
return " ".join(sorted(text.lower().split(), key=lambda x: len(x))).capitalize()
s = Solution()
print(s.one_line("Leetcode is cool"))