Sentence Shuffler
Hard
string
derangement
permutation
shuffling
Problem Description
Given a sentence, shuffle the words such that no word remains in its original position. Return any valid arrangement where each word is displaced.
Input Format
A sentence with words separated by spaces.
Output Format
Shuffled sentence where no word is in its original position, or "IMPOSSIBLE" if not possible.
Constraints
• 2 ≤ number of words ≤ 100
• Each word length ≤ 50
• No word should remain in original position
• Return any valid derangement
• Return 'IMPOSSIBLE' if no derangement exists
Sample Input/Output
Input:
the quick brown fox
Output:
quick brown fox the
Explanation
Original positions: the(0), quick(1), brown(2), fox(3). Shuffled: quick(0), brown(1), fox(2), the(3). No word in original position.
Solution Hints
125
Points
Points
3000ms
Time Limit
Time Limit
256MB
Memory Limit
Memory Limit
Code Editor
Register to Access Code Editor
Create a free account to solve coding problems and track your progress.
Register Now