java.lang.Object
g2301_2400.s2337_move_pieces_to_obtain_a_string.Solution

public class Solution extends Object
2337 - Move Pieces to Obtain a String.<p>Medium</p> <p>You are given two strings <code>start</code> and <code>target</code>, both of length <code>n</code>. Each string consists <strong>only</strong> of the characters <code>'L'</code>, <code>'R'</code>, and <code>'_'</code> where:</p> <ul> <li>The characters <code>'L'</code> and <code>'R'</code> represent pieces, where a piece <code>'L'</code> can move to the <strong>left</strong> only if there is a <strong>blank</strong> space directly to its left, and a piece <code>'R'</code> can move to the <strong>right</strong> only if there is a <strong>blank</strong> space directly to its right.</li> <li>The character <code>'_'</code> represents a blank space that can be occupied by <strong>any</strong> of the <code>'L'</code> or <code>'R'</code> pieces.</li> </ul> <p>Return <code>true</code> <em>if it is possible to obtain the string</em> <code>target</code> <em>by moving the pieces of the string</em> <code>start</code> <em><strong>any</strong> number of times</em>. Otherwise, return <code>false</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> start = &ldquo;_L__R__R_&rdquo;, target = &ldquo;L______RR&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> We can obtain the string target from start by doing the following moves:</p> <ul> <li> <p>Move the first piece one step to the left, start becomes equal to &ldquo;<strong>L</strong>___R__R_&rdquo;.</p> </li> <li> <p>Move the last piece one step to the right, start becomes equal to &ldquo;L___R___<strong>R</strong>&rdquo;.</p> </li> <li> <p>Move the second piece three steps to the right, start becomes equal to &ldquo;L______<strong>R</strong>R&rdquo;.</p> </li> </ul> <p>Since it is possible to get the string target from start, we return true.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> start = &ldquo;R_L_&rdquo;, target = &ldquo;__LR&rdquo;</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> The &lsquo;R&rsquo; piece in the string start can move one step to the right to obtain &ldquo;_<strong>R</strong>L_&rdquo;.</p> <p>After that, no pieces can move anymore, so it is impossible to obtain the string target from start.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> start = &ldquo;_R&rdquo;, target = &ldquo;R_&rdquo;</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> The piece in the string start can move only to the right, so it is impossible to obtain the string target from start.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == start.length == target.length</code></li> <li><code>1 <= n <= 10<sup>5</sup></code></li> <li><code>start</code> and <code>target</code> consist of the characters <code>'L'</code>, <code>'R'</code>, and <code>'_'</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • canChange

      public boolean canChange(String start, String target)