Problem: Move Pieces to Obtain a String

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`.


Python solution:
import math
import itertools
import bisect
import collections
import string
import heapq
import functools
import sortedcontainers
from typing import List, Dict, Tuple, Iterator

class Solution:
  def canChange(self, start: str, target: str) -> bool:
    n = len(start)
    i = 0
    j = 0

    while i <= n and j <= n:
      while i < n and start[i] == '_':
        i += 1
      while j < n and target[j] == '_':
        j += 1
      if i == n or j == n:
        return i == n and j == n
      if start[i] != target[j]:
        return False
      if start[i] == 'R' and i > j:
        return False
      if start[i] == 'L' and i < j:
        return False
      i += 1
      j += 1

    return True


Java solution:
class Solution {
  public boolean canChange(String start, String target) {
    final int n = start.length();
    int i = 0; // start's index
    int j = 0; // target's index

    while (i <= n && j <= n) {
      while (i < n && start.charAt(i) == '_')
        ++i;
      while (j < n && target.charAt(j) == '_')
        ++j;
      if (i == n || j == n)
        return i == n && j == n;
      if (start.charAt(i) != target.charAt(j))
        return false;
      if (start.charAt(i) == 'R' && i > j)
        return false;
      if (start.charAt(i) == 'L' && i < j)
        return false;
      ++i;
      ++j;
    }

    return true;
  }
}


C++ solution:
class Solution {
 public:
  bool canChange(string start, string target) {
    const int n = start.length();
    int i = 0;  // start's index
    int j = 0;  // target's index

    while (i <= n && j <= n) {
      while (i < n && start[i] == '_')
        ++i;
      while (j < n && target[j] == '_')
        ++j;
      if (i == n || j == n)
        return i == n && j == n;
      if (start[i] != target[j])
        return false;
      if (start[i] == 'R' && i > j)
        return false;
      if (start[i] == 'L' && i < j)
        return false;
      ++i;
      ++j;
    }

    return true;
  }
};