Problem · String
No Adjacent Characters
Learn this problemProblem statement
Given a string s, determine whether its characters can be rearranged so that no two adjacent characters are the same. A rearrangement uses every character of s exactly once.
Return "POSSIBLE" if the string already satisfies the condition or if such a rearrangement exists. Otherwise, return "NOT POSSIBLE".
Function
canRearrangeString(s: String) → StringExamples
Example 1
s = "aab"return = "POSSIBLE"The characters can be rearranged as "aba", which has no equal adjacent characters.
Example 2
s = "aa"return = "NOT POSSIBLE"Both characters are equal, so every rearrangement still places them next to each other.