Python – Longest Consecution without K in String
Last Updated :
12 Apr, 2023
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
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
K = 'e'
indxs = []
for idx, ele in enumerate (test_str):
if ele = = 'e' :
indxs.append(idx)
diffs = []
for idx in range ( len (indxs) - 1 ):
diffs.append(indxs[idx + 1 ] - indxs[idx])
res = max (diffs)
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
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
K = 'e'
indxs = list ( filter ( lambda ele: test_str[ele] = = 'e' , range ( len (test_str))))
diffs = [j - i for i, j in zip (indxs[: - 1 ], indxs[ 1 :])]
res = max (diffs)
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:
- Initialize a string variable named test_str.
- Initialize a character variable named K.
- Split the test_str by the character K and store the substrings into a list variable named subs.
- Find the max length of substrings from the list of subs and add 1.
- Print the result.
Python3
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
K = 'e'
subs = test_str.split(K)
res = max ( len (sub) for sub in subs) + 1
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:
- Import the required modules itertools and functools.
- Initialize the string and the character.
- Split the string into groups of consecutive characters that are not K using group by function.
- Find the length of each group using the map function.
- Find the maximum length using reduce function.
- Print the original string and the longest run calculated.
Python3
from itertools import groupby
from functools import reduce
test_str = 'geeksforgeeks is best for geeks'
K = 'e'
groups = [ list (g) for k, g in groupby(test_str, lambda x: x ! = K) if k]
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))
|
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
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
K = 'e'
indxs = list ( map ( lambda x: x[ 0 ], filter ( lambda x: x[ 1 ] = = K, enumerate (test_str))))
diffs = [indxs[i + 1 ] - indxs[i] for i in range ( len (indxs) - 1 )]
res = max (diffs)
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.
Share your thoughts in the comments
Please Login to comment...