![]() |
VOOZH | about |
Given a String and a character K, find longest substring length of K.
Input : test_str = 'abcaaaacbbaa', K = b
Output : 2
Explanation : b occurs twice, 2 > 1.Input : test_str = 'abcaacccbbaa', K = c
Output : 3
Explanation : Maximum times c occurs is 3.
Method #1: Using loop
This is brute way to solve this problem, in this, when K is encountered, counter is maintained till other character occurs, and count is noted, the maximum of these counts is kept and is returned as result.
The original string is : abcaaaacbbaa The Longest Substring Length : 4
Method #2: Using findall() + max()
In this, we get all the possible substrings of K using findall() and max() is used over it to get maximum length with len as key.
The original string is : abcaaaacbbaa The Longest Substring Length : 4
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using itertools.groupby
Step-by-step Algorithm:
The original string is : abcaaaacbbaa The Longest Substring Length : 4
Time complexity: O(n) where n is the length of the input string test_str
Space complexity: O(1)
Method #4: Using generator expression, max() and re.split() function
The original string is : abcaaaacbbaa The Longest Substring Length : 4
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 5: Using Regular expression and max()
The original string is : abcaaaacbbaa The Longest Substring Length : 4
Time complexity: O(n) where n is the length of the input string, as we need to traverse the string only once.
Auxiliary space: O(n) for storing the matches found using re.findall().
Method 6: Using numpy:
Step-by-step approach:
Output: The original string is : abcaaaacbbaa The Longest Substring Length : 4
Time Complexity:
Creating a numpy array from the input string takes O(n) time, where n is the length of the input string.
The np.where() function takes O(n) time to find the indices where the character K appears in the array.
The np.diff() function takes O(m) time, where m is the number of indices where the character K appears in the array.
The np.max() function takes O(m) time to find the maximum consecutive difference.
Therefore, the overall time complexity of the algorithm is O(n + m).
Auxiliary Space:
Creating a numpy array from the input string takes O(n) space, where n is the length of the input string.
The np.where() function returns an array of size m, where m is the number of indices where the character K appears in the array.
The np.diff() function creates a new array of size m-1.
Therefore, the overall space complexity of the algorithm is O(n + m).
Method #7: Using dynamic programming
Step-by-step approach:
The original string is : abcaaaacbbaa The Longest Substring Length : 4
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), as we are using a 1D array dp of length n to store the lengths of the longest substrings ending at each index of the input string.