java.lang.Object
g1001_1100.s1041_robot_bounded_in_circle.Solution

public class Solution extends Object
1041 - Robot Bounded In Circle.<p>Medium</p> <p>On an infinite plane, a robot initially stands at <code>(0, 0)</code> and faces north. Note that:</p> <ul> <li>The <strong>north direction</strong> is the positive direction of the y-axis.</li> <li>The <strong>south direction</strong> is the negative direction of the y-axis.</li> <li>The <strong>east direction</strong> is the positive direction of the x-axis.</li> <li>The <strong>west direction</strong> is the negative direction of the x-axis.</li> </ul> <p>The robot can receive one of three instructions:</p> <ul> <li><code>&quot;G&quot;</code>: go straight 1 unit.</li> <li><code>&quot;L&quot;</code>: turn 90 degrees to the left (i.e., anti-clockwise direction).</li> <li><code>&quot;R&quot;</code>: turn 90 degrees to the right (i.e., clockwise direction).</li> </ul> <p>The robot performs the <code>instructions</code> given in order, and repeats them forever.</p> <p>Return <code>true</code> if and only if there exists a circle in the plane such that the robot never leaves the circle.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> instructions = &ldquo;GGLLGG&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> The robot is initially at (0, 0) facing the north direction.</p> <p>&ldquo;G&rdquo;: move one step. Position: (0, 1). Direction: North.</p> <p>&ldquo;G&rdquo;: move one step. Position: (0, 2). Direction: North.</p> <p>&ldquo;L&rdquo;: turn 90 degrees anti-clockwise. Position: (0, 2). Direction: West.</p> <p>&ldquo;L&rdquo;: turn 90 degrees anti-clockwise. Position: (0, 2). Direction: South.</p> <p>&ldquo;G&rdquo;: move one step. Position: (0, 1). Direction: South.</p> <p>&ldquo;G&rdquo;: move one step. Position: (0, 0). Direction: South.</p> <p>Repeating the instructions, the robot goes into the cycle: (0, 0) &ndash;> (0, 1) &ndash;> (0, 2) &ndash;> (0, 1) &ndash;> (0, 0).</p> <p>Based on that, we return true.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> instructions = &ldquo;GG&rdquo;</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> The robot is initially at (0, 0) facing the north direction.</p> <p>&ldquo;G&rdquo;: move one step. Position: (0, 1). Direction: North.</p> <p>&ldquo;G&rdquo;: move one step. Position: (0, 2). Direction: North.</p> <p>Repeating the instructions, keeps advancing in the north direction and does not go into cycles.</p> <p>Based on that, we return false.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> instructions = &ldquo;GL&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> The robot is initially at (0, 0) facing the north direction.</p> <p>&ldquo;G&rdquo;: move one step. Position: (0, 1). Direction: North.</p> <p>&ldquo;L&rdquo;: turn 90 degrees anti-clockwise. Position: (0, 1). Direction: West.</p> <p>&ldquo;G&rdquo;: move one step. Position: (-1, 1). Direction: West.</p> <p>&ldquo;L&rdquo;: turn 90 degrees anti-clockwise. Position: (-1, 1). Direction: South.</p> <p>&ldquo;G&rdquo;: move one step. Position: (-1, 0). Direction: South.</p> <p>&ldquo;L&rdquo;: turn 90 degrees anti-clockwise. Position: (-1, 0). Direction: East.</p> <p>&ldquo;G&rdquo;: move one step. Position: (0, 0). Direction: East.</p> <p>&ldquo;L&rdquo;: turn 90 degrees anti-clockwise. Position: (0, 0). Direction: North.</p> <p>Repeating the instructions, the robot goes into the cycle: (0, 0) &ndash;> (0, 1) &ndash;> (-1, 1) &ndash;> (-1, 0) &ndash;> (0, 0).</p> <p>Based on that, we return true.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= instructions.length <= 100</code></li> <li><code>instructions[i]</code> is <code>'G'</code>, <code>'L'</code> or, <code>'R'</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • isRobotBounded

      public boolean isRobotBounded(String instructions)