You get a list of words, words. Return the words sorted from shortest to longest.
Words of the same length go in alphabetical order, from A to Z.
A word can appear more than once. Every copy stays in the result.
Example 1:
Input: words = ["banana", "fig", "apple", "kiwi"] Output: ["fig", "kiwi", "apple", "banana"] Explanation: "fig" has 3 letters and "kiwi" has 4. "apple" has 5 and "banana" has 6.
Example 2:
Input: words = ["dog", "cat", "ant", "bee"] Output: ["ant", "bee", "cat", "dog"] Explanation: All four words have 3 letters. So they go from A to Z.
Example 3:
Input: words = ["sun", "a", "moon", "sun"] Output: ["a", "sun", "sun", "moon"] Explanation: "sun" appears twice, so it stays twice. Both copies come after "a" and before "moon".
Constraints:
wordshas 1 to 100 words.- Each word has 1 to 20 lowercase letters.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
