Sometimes, while working with Strings, we can have a problem in which we need to perform the extraction of length of longest consecution of certain letter. This can have application in web development and competitive programming. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop
This is brute force method in which we can perform this task. In this, we run a loop over the String and keep on memorizing the maximum whenever the run occurs.
# Python3 code to demonstrate working of # Longest Run of Character in String # Using loop # initializing string test_str = 'geeksforgeeeks' # printing original string print("The original string is : " + test_str) # initializing K K = 'e' # Longest Run of Character in String # Using loop res = 0cnt = 0for chr in test_str: if chr == K: cnt += 1 else: res = max(res, cnt) cnt = 0res = max(res, cnt) # printing result print("Longest Run length of K : " + str(res)) |
The original string is : geeksforgeeeks Longest Run length of K : 3
Method #2 : Using max() + re.findall()
This is one liner way in which this problem can be solved. In this, we find the maximum of all runs found using findall().
# Python3 code to demonstrate working of # Longest Run of Character in String # Using max() + re.findall() import re # initializing string test_str = 'geeksforgeeeks' # printing original string print("The original string is : " + test_str) # initializing K K = 'e' # Longest Run of Character in String # Using max() + re.findall() res = len(max(re.findall(K + '+', test_str), key = len)) # printing result print("Longest Run length of K : " + str(res)) |
The original string is : geeksforgeeeks Longest Run length of K : 3
Recommended Posts:
- Run Length Encoding in Python
- Run Python script from Node.js using child process spawn() method
- How to run Python code on Google Colaboratory
- Python | Making program run faster
- Run python script from anywhere in linux
- How to Run a Python Script
- Python IMDbPY – Getting run time of the series
- Run Python Script using PythonShell from Node.js
- Python IMDbPY – Getting Series Run times as XML
- How to Compile and Run C/C++/Java Programs in Linux
- Guide to deploy containers on Google Cloud Run
- How to create an instance of a Metaclass that run on both Python2 and Python3?
- Python | Insert character after every character pair
- Python program to read character by character from a file
- Python program to find occurrence to each character in given string
- Python | Find position of a character in given string
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Python | Remove List elements containing given String character
- Kth most frequent Character in a given String
- Python | Check order of character in string using OrderedDict( )
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.

