Rearrange a String So Adjacent Characters Differ
Updated
Problem statement
Rearrange all characters of s so no two adjacent characters are equal.
Return the lexicographically smallest valid rearrangement: compare strings from left to right, and at the first difference choose the smaller character. Return the empty string when no valid rearrangement exists. Character comparisons use ASCII order.
Function
rearrangeWithoutAdjacentEquals(s: String) → StringExamples
Example 1
s = "aab"return = "aba"aba is valid and is the only arrangement without equal neighbors.
Example 2
s = "aaab"return = ""Three copies of a cannot be separated by one b.
Example 3
s = "aabbcc"return = "abacbc"This is the lexicographically smallest valid use of all characters.
Constraints
0 <= s.length <= 2000.scontains ASCII letters.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Reorganize a StringAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Greedy and Beam Sentence DecodingMicrosoft · PHONE SCREEN · ONSITE INTERVIEW
- Worker Task Assignment Part 1 - Balance WorkloadStripe · ONSITE INTERVIEW
- Lexicographically Smallest School AssemblyWells Fargo · OA
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEW
- Rearrange a String So No Neighbors MatchThe D.E. Shaw Group · ONSITE INTERVIEW
- Longest-Match Tokenizer with Unknown SymbolsAnthropic · ONSITE INTERVIEW
- Minimum Nonadjacent Flips for an Alternating Binary StringInfosys · ONSITE INTERVIEW
- Shortest Sequence of Source SubsequencesPinterest · ONSITE INTERVIEW
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEW