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 number of sub-strings of a string such that all are power of 5
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum rotations required to get the same String | Set-2
- 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.
- Count of lines required to write the given String
- Minimum changes required such that the string satisfies the given condition
- Operations required to make the string empty
- Repeat substrings of the given String required number of times
- Minimum swaps required to convert one binary string to another
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.