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...
Microsoft Corporation SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter