Leetcode 2337: Move Pieces to Obtain a String

grid47
grid47
Exploring patterns and algorithms
Mar 18, 2024 6 min read

You are given two strings start and target, each consisting of the characters 'L', 'R', and '_'. The goal is to check if it is possible to transform the string start into target by moving the ‘L’ and ‘R’ characters. ‘L’ can only move to the left and ‘R’ can only move to the right. A blank space can be occupied by either ‘L’ or ‘R’.
Problem
Approach
Steps
Complexity
Input: The input consists of two strings, `start` and `target`, each of length `n`.
Example: start = '_L__R__R_', target = 'L______RR'
Constraints:
• 1 <= n <= 10^5
• start and target consist of the characters 'L', 'R', and '_'.
Output: Return `true` if it is possible to obtain the target string from the start string, otherwise return `false`.
Example: true
Constraints:
Goal: Determine if it is possible to transform `start` into `target` by moving pieces according to the described rules.
Steps:
• Initialize two queues to store the positions of the 'L' and 'R' pieces in `start` and `target`.
• Compare the characters in both queues to ensure they match.
• Ensure that the 'L' pieces in `start` do not move right in `target` and the 'R' pieces do not move left.
Goal: The strings are of equal length and consist only of 'L', 'R', and '_'. The length `n` can be up to 100,000.
Steps:
• 1 <= n <= 10^5
• start and target consist of the characters 'L', 'R', and '_'.
Assumptions:
• The input strings are valid and consist only of 'L', 'R', and '_'.
• The strings are of equal length.
Input: start = '_L__R__R_', target = 'L______RR'
Explanation: The goal is to check if it's possible to transform the start string into the target string by moving 'L' and 'R' pieces according to the rules. This is possible, as described in the explanation.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus