Skip to main content

Jump Game VII

Medium
Data Structures and Algorithms
Editorial
Topics:
Array
String
Sliding Window

You are given a binary string s, with positions numbered from 0 through s.length - 1. Position 0 is the starting position, and the destination is the final position, s.length - 1. A position is open when its character is '0'; a position containing '1' cannot be landed on.

From a position i, you may move forward to a position j only when minJump <= j - i <= maxJump. Every position on which you land must be open. The starting position is already occupied, so it does not require a jump.

Implement canReach and return true if the destination can be reached from position 0; otherwise, return false.

Example 1:

Input: s = "00100100", minJump = 2, maxJump = 3
Output: false
Explanation: Position 3 is reachable from position 0, and position 6 is then reachable from position 3. However, the destination at position 7 is four steps from position 3 and one step from position 6, so it cannot be reached with jumps of length 2 through 3.

Example 2:

Input: s = "0101100", minJump = 2, maxJump = 3
Output: false
Explanation: Position 2 is reachable from position 0, but the next possible positions contain '1' or lead past the destination. Thus the final position cannot be reached.

Example 3:

Input: s = "0000000", minJump = 3, maxJump = 4
Output: true
Explanation: A valid route is position 0 to position 3, followed by position 3 to the destination at position 6.

Constraints:

  • 2 <= s.length <= 10^5
  • s consists only of the characters '0' and '1'
  • s[0] == '0'
  • 1 <= minJump <= maxJump < s.length

Need help or want more practice?

Practise in an interviewGet AI Coach help