304 North Cardinal St.
Dorchester Center, MA 02124

# Move Pieces to Obtain a String LeetCode Solution

## Problem – Move Pieces to Obtain a String LeetCode Solution

You are given two strings `start` and `target`, both of length `n`. Each string consists only of the characters `'L'``'R'`, and `'_'` where:

• The characters `'L'` and `'R'` represent pieces, where a piece `'L'` can move to the left only if there is a blank space directly to its left, and a piece `'R'` can move to the right only if there is a blank space directly to its right.
• The character `'_'` represents a blank space that can be occupied by any of the `'L'` or `'R'` pieces.

Return `true` if it is possible to obtain the string `target` by moving the pieces of the string `start` any number of times. Otherwise, return `false`.

Example 1:

``````Input: start = "_L__R__R_", target = "L______RR"
Output: true
Explanation: We can obtain the string target from start by doing the following moves:
- Move the first piece one step to the left, start becomes equal to "L___R__R_".
- Move the last piece one step to the right, start becomes equal to "L___R___R".
- Move the second piece three steps to the right, start becomes equal to "L______RR".
Since it is possible to get the string target from start, we return true.
``````

Example 2:

``````Input: start = "R_L_", target = "__LR"
Output: false
Explanation: The 'R' piece in the string start can move one step to the right to obtain "_RL_".
After that, no pieces can move anymore, so it is impossible to obtain the string target from start.
``````

Example 3:

``````Input: start = "_R", target = "R_"
Output: false
Explanation: The piece in the string start can move only to the right, so it is impossible to obtain the string target from start.
``````

Constraints:

• `n == start.length == target.length`
• `1 <= n <= 105`
• `start` and `target` consist of the characters `'L'``'R'`, and `'_'`.

### Move Pieces to Obtain a String LeetCode Solution in Java

``````    public boolean canChange(String start, String target) {
if (!(start.replaceAll("_", "")).equals(target.replaceAll("_", ""))) {
return false;
}
for (int i = 0, j = 0, n = start.length(); i < n && j < n; ++i, ++j) {
while (i < n && start.charAt(i) == '_') {
++i;
}
while (j < n && target.charAt(j) == '_') {
++j;
}
if (i < n && j < n && (start.charAt(i) == 'L' && i < j || target.charAt(j) == 'R' && i > j)) {
return false;
}
}
return true;
}
``````

### Move Pieces to Obtain a String LeetCode Solution in C++

``````class Solution {
public:
bool canChange(string s, string t) {
queue<pair<char, int>> ss, ts;

// Fill queue with start
for(int i=0; i<s.size(); i++){
if(s[i] != '_')ss.push({s[i], i});
}

// Fill queue with target
for(int i=0; i<t.size(); i++){
if(t[i] != '_')ts.push({t[i], i});
}

if(ss.size() != ts.size())return false;

while(ss.size()){
pair<char, int> sp, tp;

sp = ss.front();
tp = ts.front();

ss.pop();
ts.pop();

//             If both the letters don;t match return false, or check for the index to move Left and Right
if(sp.first != tp.first) return false;
if(sp.first == 'L' && tp.second>sp.second){
return false;
} else if(sp.first == 'R' && tp.second<sp.second){
return false;
}
}

return true;
}
};
``````

### Move Pieces to Obtain a String LeetCode Solution in Python

``````class Solution:
def canChange(self, A: str, B: str) -> bool:
P = lambda c    :  c != '_'
I = lambda s,x  :  [i for i,c in enumerate(s) if c==x]
G = lambda d,p  :  all( p(x,y) for x,y in zip( I(A,d), I(B,d) ) )
S = lambda      :  [*filter(P,A)] == [*filter(P,B)]
return S() and G('L', ge) and G('R', le)
#      1.          2.             3.
``````
##### Move Pieces to Obtain a String LeetCode Solution Review:

In our experience, we suggest you solve this Move Pieces to Obtain a String LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Move Pieces to Obtain a String LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Move Pieces to Obtain a String LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions