Python – Longest Substring Length of K

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.



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Longest Substring of K
# Using loop
  
# initializing string
test_str = 'abcaaaacbbaa'
  
# printing original String
print("The original string is : " + str(test_str))
  
# initializing K 
K = 'a'
  
cnt = 0
res = 0
for idx in range(len(test_str)):
      
    # increment counter on checking
    if test_str[idx] == K:
        cnt += 1
    else:
        cnt = 0
          
    # retaining max
    res = max(res, cnt)
  
# printing result 
print("The Longest Substring Length : " + str(res)) 

chevron_right


Output

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.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Longest Substring of K
# Using findall() + max()
import re
  
# initializing string
test_str = 'abcaaaacbbaa'
  
# printing original String
print("The original string is : " + str(test_str))
  
# initializing K 
K = 'a'
  
# getting all substrings
res = re.findall(r'' + K + '+', test_str)
  
# getting maximum of substrings Length
res = len(max(res, key = len))
  
# printing result 
print("The Longest Substring Length : " + str(res)) 

chevron_right


Output

The original string is : abcaaaacbbaa
The Longest Substring Length : 4

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.