Check if the end of the given Binary string can be reached by choosing the jump value in between the given range


A binary string is a string that contains only two different types of characters that are 0 and 1. We are given a binary string and two integers L and the R. We can make the jump of size between ‘L’ and ‘R’ length both inclusive and only from the index where the value of the string is ‘0’. We have to start from the Zeroth index and find out whether we can reach the last index or not.

Sample Examples

Input1:
string str = “01001110010”
int L = 2, R = 4
Output: Yes, we can reach the last index.

Explanation

We can jump of three from the Zeroth index and then two jumps of the 4, and this way we can reach the final required index.

Input2:
string str = “01001110010”
int L = 2, R = 3
Output: No, we cannot reach the last index. 

Explanation

We can two jumps either of 2 or 3, if we jump from the zeroth index we will either reach, the 2nd or 3rd index, and from there we can only go to the index with the value ‘1’ and no further jump can be made.

Approach 1

Idea

The idea is to apply the concepts of dynamic programming. We can maintain the array which indicates that a position can be reached or not.

If we can reach a position then for the next l to r positions can also be achieved.

Implementation

We will create a function that will take the string, the left position, and the right position as the parameters and return the bool value.

In the function we will traverse over the array and using the nested for loop we can traverse over the range and check if the current position minus the current range position is reachable then we can reach at this position also.

At the end we will return the status of the last index of the DP array represents the final answer.

Example

#include <bits/stdc++.h>
using namespace std;
// function to implement the DP concepts 
bool check(string str, int l, int r){
   int len = str.length(); // length of the string     
   vector<int>dp(len); // vector to store the states results 
   
   // Initiating the first index 
   dp[0] = str[0] == '0'; 
   
   // traversing over the array 
   for(int i=1; i<len; i++ ){
      for(int j = l; j <= r ; j++){
         if(i-j < 0){
            break;
         }
         if(str[i-j] == '1' || dp[i-j] == 0){
            continue;
         }
         else{
            dp[i] = 1;
            break;
         }
      }
   }
   return dp[len-1];
}
int main(){
   string str = "01001110010";
   int l = 2, r = 4; 
   
   // calling the function 
   int res = check(str, l, r);    
   if(res > 0){
      cout<<"Yes, we can reach the end of the string by starting from the zeroth index and jumping in the given range"<<endl;
   }
   else{
      cout<<"No, we cannot reach the end of the string by starting from the zeroth index and jumping in the given range"<<endl;
   }
}

Output

Yes, we can reach the end of the string by starting from the zeroth index and jumping in the given range

Time and Space Complexity

The time complexity of the above code is O(N^2), where N is the size of the given string. We are using the nested for loop, this makes the time complexity of N^2.

The space complexity of the above code is O(N), as we are using an array of length N to store the DP states.

Approach 2

This approach is the better version of the previous one, in this we will maintain an integer to get the count of number of jumps we can make. At each, if jump is possible we can increase the count and if at any position if the jump is not possible we can decrease it.

We will store the data at each index of the DP array and will use it later.

Example

#include <bits/stdc++.h>
using namespace std;
bool check(string str, int l, int r){
   int len = str.length(); // length of the string     
   vector<int>dp(len); // vector to store the states results     
   // initiating the first index 
   dp[0] = str[0] == '0';    
   int count = 0; // variable to maintain the count of the jump 
   
   // traversing over the array 
   for(int i=1; i<len; i++ ){
      if (i >= l) {
         count += dp[i - l];
      }
      if (i > r) {
         count -= dp[i -r- 1];
      }
      dp[i] = (count > 0) and (str[i] == '0');
   }
   return dp[len-1];
}

// main function 
int main(){
   string str = "01001110010";
   int l = 2, r = 4;  
   
   // calling the function 
   int res = check(str, l, r);    
   if(res > 0){
      cout<<"Yes, we can reach the end of the string by starting from the zeroth index and jumping in the given range"<<endl;
   } else {
      cout<<"No, we cannot reach the end of the string by starting from the zeroth index and jumping in the given range"<<endl;
   }
}

Output

Yes, we can reach the end of the string by starting from the zeroth index and jumping in the given range

Time and Space Complexity

The time complexity of the above code is O(N), where N is the size of the given string. We are using the single loop to traverse over the string makes the time complexity linear.

The space complexity of the above code is O(N), as we are using an array of length N to store the DP states.

Conclusion

In this tutorial, we have implemented a code find whether we can reach to the end of the given string by starting from the first index and moving given number of indexes from the index where given string contains ‘0’. We have implemented the Dynamic Programming approach with O(N^2) and O(N) time complexity and O(N) space complexity.

Updated on: 26-Jul-2023

27 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements