Every Arrangement of Repeated Tiles
You have a row of numbered tiles tiles, and some numbers may repeat. Two arrangements are the same if they read the same left to right.
Return every distinct arrangement of all the tiles, sorted in ascending lexicographic order (compare arrangements number by number from the left).
Example 1
Input: tiles = [1,1,2] Output: [[1,1,2],[1,2,1],[2,1,1]] Explanation: Only three distinct orders exist because the two 1s are interchangeable.
Example 2
Input: tiles = [3,1,2] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Constraints
- 1 <= tiles.length <= 8
- -10 <= tiles[i] <= 10
Hint 1
Sort the tiles so equal numbers sit next to each other.
Hint 2
At each position, skip a tile if it equals the previous tile and that previous tile is not in use; this removes duplicates.
Discussion 0
Join free to post in the discussion.
Good practice for: Game Developer, Backend Developer, Full Stack Developer, Automation Test Engineer, Data Scientist
Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.