Rearrange String Problem Statement
Given a string ‘S’, your task is to rearrange its characters so that no two adjacent characters are the same. If it's possible, return any such arrangement, otherwise return “not possible”.
Example:
Input:
S = "qaacde"
Output:
"qacade"
Explanation:
In the string "qaacde", originally there are two same adjacent characters. One possible rearrangement where no two adjacent characters are the same is "qacade".
Input:
T
S1
S2
…
ST
Output:
"Yes" if rearrangement is possible, otherwise "No" for each test case.
Constraints:
- 1 <= T <= 10
- 0 <= |S| <= 105
- Time Limit: 1 second.
Note:
No need to print anything; implementation of the function is sufficient.
Be the first one to answer
Add answer anonymously...
Top Microsoft Corporation SDE-2 interview questions & answers
Popular interview questions of SDE-2
Top HR questions asked in Microsoft Corporation SDE-2
>
Microsoft Corporation SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app