Python program to find maximum uppercase run
Giving a String, write a Python program to find the maximum run of uppercase characters.
Examples:
Input : test_str = ‘GeEKSForGEEksISBESt’
Output : 5
Explanation : ISBES is best run of uppercase.Input : test_str = ‘GeEKSForGEEKSISBESt’
Output : 10
Explanation : GEEKSISBES is best run of uppercase.
Method : Using isupper() + loop
In this, we update the maximum run when non-uppercase is encountered otherwise counter is incremented if the character is uppercase.
Python3
# Python3 code to demonstrate working of# Maximum uppercase run# Using isupper() + loop# initializing stringtest_str = 'GeEKSForGEEksIsBESt'# printing original stringprint("The original string is : " + str(test_str))cnt = 0res = 0for idx in range(0, len(test_str)): # updating run count on uppercase if test_str[idx].isupper(): cnt += 1 # on lowercase, update the maxrun else : if res < cnt : res = cnt cnt = 0 else : cnt = 0if test_str[len(test_str) - 1].isupper(): res = cnt# printing resultprint("Maximum Uppercase Run : " + str(res)) |
Output:
The original string is : GeEKSForGEEksISBESt Maximum Uppercase Run : 5
The time and space complexity for all the methods are the same:
Time Complexity : O(n)
Auxiliary Space : O(n)


