Shortest Words First
Return a new list with the words of words ordered from shortest to longest. Words of the same length keep the order they had in words, so the sort must be stable. Do not change words.
An empty list gives an empty list.
Example 1
words ["pear","fig","banana","kiwi"]
returns ["fig","pear","kiwi","banana"]
fig has 3 letters, pear and kiwi have 4 (pear came first), banana has 6.
Example 2
words ["bb","a","cc","d"]
returns ["a","d","bb","cc"]
a and d have one letter and keep their order; bb and cc keep theirs.
Constraints
0 <= len(words) <= 501 <= len(words[i]) <= 12- Lowercase letters only; repeated words are allowed
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Sorting and searching problems
- Sort by Last DigitBeginner
- Strictly Increasing CheckBeginner
- Top Three ValuesBeginner
- Where It LandsBeginner
- Widest Gap After SortingBeginner
- Binary SearchEasy
- Can Attend All MeetingsEasy
- Can Make Arithmetic ProgressionEasy