1327. Dictionary Order Numbers
An archive labels boxes with numbers, but the shelves are arranged the way a dictionary arranges words, character by character. Given the array nums of non-negative integers, return the same values ordered by the lexicographic order of their decimal strings. Characters are compared left to right by digit, and a string that is a proper prefix of another comes first, so the order of 1, 10, 100, 2 and 9 is exactly 1, 10, 100, 2, 9. Equal values stay adjacent.
This is not numeric order: 100 comes before 2. A most-significant-digit radix sort over the digit strings is the intended idea, though sorting with a string comparator also fits the limits. The cost is O(n * d * log n) with a comparator, or O(n * d) with radix, where d is at most 10, and O(n) space.
Example 1
- Input:
- nums = [9,100,1,10,2]
- Output:
- [1,10,100,2,9]
- Explanation:
As strings the values order as "1", "10", "100", "2", "9".
Example 2
- Input:
- nums = [0,5,50,500,49]
- Output:
- [0,49,5,50,500]
- Explanation:
The strings "0", "49", "5", "50", "500" are in dictionary order, with "5" before its extensions "50" and "500".
Example 3
- Input:
- nums = [1000000000,1,10,100000000]
- Output:
- [1,10,100000000,1000000000]
- Explanation:
"1" is a prefix of "10", which is a prefix of "100000000", which is a prefix of "1000000000".
Constraints
- 1 ≤ n ≤ 1000
- 0 ≤ nums[i] ≤ 109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n * d * log n)
- Space
- O(n)