Problem · String

No Adjacent Characters

Learn this problem
EasyNutanix logoNutanixOA

Problem 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) → String

Examples

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.

More Nutanix problems

drafts saved locally
public String canRearrangeString(String s) {
  // write your code here
}
s"aab"
expected"POSSIBLE"
checking account