Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
You are given a string as an input. Rearrange the characters of the string such that no character appears in its original position.
If the rearrangement is possible, print the rearranged string else print 'Not possible'.

Sample Input:
Case 1:
aabccbadd

Case 2:
abaab

Sample Output:
Case 1:
dcaabcdab(It is one of the possible valid rearrangements of 'aabccbadd')

Case 2:
Not possible

What I have tried:

i tried to rearrange ,but facing sum difficulty ,what logic should be used.
Posted
Updated 30-Apr-16 19:48pm
Comments
Mehdi Gholam 1-May-16 1:10am    
Looks like homework.
Patrice T 1-May-16 2:54am    
HomeWork!
If you show code and explain a particular problem, we will help.

1 solution

Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A very quick search using the beginning of your subject as the search term gave half a million hits: Rearrange the characters of the string such that no character appears in its original position - Google Search[^]
So your homework has been searched for - and done - many times before.
The top link gives you the algorithm, and full code!
Rearrange a string so that all same characters become d distance away - GeeksforGeeks[^]
But...do bear in mind that your tutor has access to Google as well, and will recognise stolen code... :laugh:

In future, please try to do at least basic research yourself, and not waste your time or ours.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900