Open In App

Python – Longest Consecution without K in String

Last Updated : 12 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a String, find the length of the longest consecution, in which K doesn’t appear.

Input : test_str = ‘geeksforgeeks is best for geeks’, K = ‘e’
Output : 9
Explanation : from s in best to e in geeks, 9th letter is “e”.

Input : test_str = ‘geeksforgeeks’, K = ‘e’
Output : 7
Explanation : from k to e, 7th letter is e, longest run.

Method #1 : Using a loop

We perform this in 2 steps, in 1st we iterate for all elements to get indices of K, and then in the next step find the maximum difference between consecutive characters.

Python3




# Python3 code to demonstrate working of
# Longest Consecution without K in String
# Using loop
 
# initializing string
test_str = 'geeksforgeeks is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = 'e'
 
# getting all indices
indxs = []
for idx, ele in enumerate(test_str):
    if ele == 'e':
        indxs.append(idx)
 
# getting difference
diffs = []
for idx in range(len(indxs) - 1):
    diffs.append(indxs[idx + 1] - indxs[idx])
 
# getting max diff using max()
res = max(diffs)
 
# printing result
print("Longest run : " + str(res))


Output

The original string is : geeksforgeeks is best for geeks
Longest run : 9

Method #2 : Using filter() + lambda + zip() + list comprehension + max()

In this, we get the index of K elements using filter() + lambda, and zip() + list comprehension is used to get differences between indices. Post that, max() is used for extracting the maximum difference.

Python3




# Python3 code to demonstrate working of
# Longest Consecution without K in String
# Using filter() + lambda + zip() + list comprehension + max()
 
# initializing string
test_str = 'geeksforgeeks is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = 'e'
 
# getting all indices using filter + lambda
indxs = list(filter(lambda ele: test_str[ele] == 'e', range(len(test_str))))
 
# getting difference using zip()
# negative index, for getting successive elements
diffs = [j - i for i, j in zip(indxs[: -1], indxs[1 :])]
 
# getting max diff
res = max(diffs)
 
# printing result
print("Longest run : " + str(res))


Output

The original string is : geeksforgeeks is best for geeks
Longest run : 9

The Time and Space Complexity for all the methods is the same:

Time Complexity: O(n)

Space Complexity: O(n)

Method #3: Using str.split() function

Algorithm:

  1. Initialize a string variable named test_str.
  2. Initialize a character variable named K.
  3. Split the test_str by the character K and store the substrings into a list variable named subs.
  4. Find the max length of substrings from the list of subs and add 1.
  5. Print the result.

Python3




# initializing string
test_str = 'geeksforgeeks is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = 'e'
 
# splitting string into substrings without K
subs = test_str.split(K)
 
# getting max length substring using max() and len()
res = max(len(sub) for sub in subs) + 1
 
# printing result
print("Longest run : " + str(res))


Output

The original string is : geeksforgeeks is best for geeks
Longest run : 9

Time complexity: O(n) where n is the length of the input string test_str. 
Space complexity: O(n) where n is the length of the input string test_str. 

Method #4: using itertools and reduce method:

Algorithm:

  1. Import the required modules itertools and functools.
  2. Initialize the string and the character.
  3. Split the string into groups of consecutive characters that are not K using group by function.
  4. Find the length of each group using the map function.
  5. Find the maximum length using reduce function.
  6. Print the original string and the longest run calculated.

Python3




# Python program for the above approach
from itertools import groupby
from functools import reduce
 
# Driver Code
 
test_str = 'geeksforgeeks is best for geeks'
K = 'e'
 
# Split the string into groups of
# consecutive characters that are not K
groups = [list(g) for k, g in groupby(test_str, lambda x: x != K) if k]
 
# Get the length of each group and
# find the maximum length
res = reduce(lambda acc, x: max(acc, len(x)), groups, 0)+1
 
print("The original string is : " + str(test_str))
print("Longest run : " + str(res))
 
# This code is contributed by Jyothi Pinjala


Output

The original string is : geeksforgeeks is best for geeks
Longest run : 9

Time Complexity: O(N), The time complexity of the code is O(n) where n is the length of the input string. This is because we iterate through the input string only once.
Space Complexity: O(N), The space complexity of the code is O(n) where n is the length of the input string. This is because we create a list of groups that can have a length of at most n. The other variables created have constant space requirements.
has context menu

Method #4: Using map filter enumerate and lambda

  • Initializing the list and k
  • Using map(), a list of indices of the given character is obtained and stored in the ‘indxs’ variable.
  • Using filter(), the values in the ‘indxs’ list that are greater than or equal to the length of the input string minus 1 are removed. The resulting list is then converted to a set and stored in the ‘indxs_set’ variable.
  • Using max(), the maximum value in the ‘diffs
  • Printing the result

Python3




# initializing string
test_str = 'geeksforgeeks is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = 'e'
 
# finding all the indices of K in the string
indxs = list(map(lambda x: x[0], filter(lambda x: x[1] == K, enumerate(test_str))))
 
# finding the difference between the consecutive indices
diffs = [indxs[i+1]-indxs[i] for i in range(len(indxs)-1)]
 
# getting the longest consecution without K
res = max(diffs)
 
# printing the result
print("Longest run : " + str(res))


Output

The original string is : geeksforgeeks is best for geeks
Longest run : 9

Time complexity: O(n), where n is the length of the input string.

Auxiliary Space: O(n), where n is the length of the input string.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads