Given a string consisting of 1 and 0, the task is to find out the maximum power of jump required to reach the end of the string. At a time you can jump from one 1 to next 1 or from one 0 to next 0.
Note: Power of jump is defined as the distance between two consecutive 1’s or two consecutive 0’s.
Input: 10101 Output: 2 First, make a power jump of 1 to reach first 1, then a power jump of 2 to reach second 1 and then finally a power jump of 2 to reach the end of the string hence the maximum power of jump is 2. Input: 11110 Output: 5 Since to reach the end of the string, we have to make power jump of 5 to reach 0 and end of the string
- Check if the last character of given string is 1 or 0.
- If the last character is 1 then search for the first 1 in the string and continue to jump to the next 1 to reach the last of the string. Update the maximum jumps.
- Similarly, if the last character is 0 then search for the first 0 in the string and continue to jump to the next 0 to reach the last of the string. Update the maximum jumps.
- Return the maximum number of jumps taken to reach the end.
# Python3 programme to calculate
# power of jump
# Function to calculate the maximum
# power of the jump
# Initialize the count with 1
count = 1
max_so_far = 0
# Find the character at last index
ch = s[-1]
# Start traversing the string
for i in range(0, len(s)):
# Check if the current char is
# equal to the last character
if s[i] == ch:
# max_so_far stores maximum value of
# the power of the jump from starting
# to ith position
if count > max_so_far:
max_so_far = count
# Reset the count to 1
count = 1
# Else, increment the number
# of jumps/count
count += 1
# Return the maximum number of jumps
# Driver Code
if __name__ == “__main__”:
st = “1010101”
# This code is contributed
# by Rituraj Jain
Time Compexity : O(n) where n is the length of the string
- Minimum number of given operations required to convert a string to another string
- Minimum changes required to make first string substring of second string
- Minimum moves to reach from i to j in a cyclic string
- Find time taken for signal to reach all positions in a string
- Find sub-string with given power
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum number of sub-strings of a string such that all are power of 5
- Minimum rotations required to get the same string
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Operations required to make the string empty
- Minimum changes required such that the string satisfies the given condition
- Count of lines required to write the given String
- Minimum swaps required to convert one binary string to another
- Minimum number of operations required to sum to binary string S
- Minimum swaps required to make a binary string alternating
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.