You are given a string s and two distinct lowercase English letters x and y.
Rearrange the characters of s to construct a new string t such that:
t is a permutation of s.y appears before every occurrence of x in t.Return any valid string t.
Example 1:
Input: s = "aabc", x = "a", y = "c"
Output: "cbaa"
Explanation:
The string "cbaa" is a permutation of "aabc", and every occurrence of 'c' appears before every occurrence of 'a'.
Example 2:
Input: s = "dcab", x = "d", y = "b"
Output: "cabd"
Explanation:
The string "cabd" is a permutation of "dcab", and every occurrence of 'b' appears before every occurrence of 'd'.
Example 3:
Input: s = "axe", x = "o", y = "x"
Output: "axe"
Explanation:
The string "axe" is already valid. Since 'o' does not occur in the string, the required condition is automatically satisfied.
Constraints:
1 <= s.length <= 100s consists of lowercase English letters.x and y are lowercase English letters.x != yProblem Overview: You need to rearrange characters in a string so a restricted character pair does not appear next to each other. The challenge is preserving all characters while building a valid ordering. Most solutions rely on frequency tracking and careful placement of characters during iteration.
Approach 1: Generate All Permutations (O(n!))
The brute force approach generates every possible permutation of the string and checks whether the resulting arrangement contains the forbidden adjacent pair. You iterate through each permutation and validate neighboring characters with a simple scan. This approach is useful for understanding the constraint, but it becomes unusable once the string length grows beyond small test cases. Space complexity is O(n) for recursion and temporary storage.
Approach 2: Greedy Frequency Placement with Sorting (O(n log n))
A more practical method counts character frequencies using a hash map, sorts characters by frequency, then places them strategically to avoid invalid adjacency. The key insight is to distribute high-frequency characters first so they do not cluster into restricted pairs later. You repeatedly append a valid character while tracking the previously placed character and the restricted combination. This approach works well when the alphabet size is limited and is commonly implemented with arrays or hash maps. Time complexity is O(n log k), where k is the number of distinct characters, and space complexity is O(k).
Approach 3: Max Heap Greedy (O(n log k))
The optimal interview approach uses a max heap to always pick the character with the highest remaining frequency that does not violate the adjacency rule. You pop the best candidate, append it to the result, then temporarily hold the previously used character until it becomes valid again. This prevents invalid pairs while ensuring characters are consumed efficiently. Heap operations keep insertion and extraction fast, making this approach scalable for larger inputs. It combines greedy algorithms with priority queues. Time complexity is O(n log k) and space complexity is O(k).
Recommended for interviews: Interviewers typically expect the heap-based greedy solution because it demonstrates control over ordering constraints and efficient frequency management. Showing the brute force approach first proves you understand the problem definition, but moving to the heap optimization shows stronger algorithmic thinking and better scalability analysis.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Generate All Permutations | O(n!) | O(n) | Tiny inputs or brute force validation |
| Greedy Frequency Placement | O(n log k) | O(k) | General-purpose solution with manageable character set |
| Max Heap Greedy | O(n log k) | O(k) | Best interview solution and large-scale inputs |
3992. Rearrange String to Avoid Character Pair (Leetcode Easy) • Programming Live with Larry • 64 views views
Watch 2 more video solutions →Practice Rearrange String to Avoid Character Pair with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor