I have been trying to deal with this problem set, and I have figured out how to solve it as well. But there is one problem that is Timeout error for some hidden cases, but no wrong answers
PROBLEM STATEMENT
In this challenge, a string and a list of intervals are given. The string consists of English letters only and it can contain both lowercase and uppercase letters.
For two different letters, we say that the first letter is greater than the second letter when the first letter comes later in the alphabet than the second letter ignoring the case of the letters. For example, the letter 'Z' and 't' are greater than the letters 'b' and 'G', while the letters 'B' andd 'b' are equal as case is not considered.
The task is the following. For each given interval, you need to find the count of the greatest letter occurring in the string in that interval, ignoring the case of the letters, so occurrences of, for example, a and A are occurrences of the same letter.
Consider, for example, for the string "AbaBacD". In the interval, [0, 4], the greatest letter is 'b' with count 2.
Input Format
The first line contains integer N, denoting the length of the input string.
The second line contains string S.
The third line contains an integer Q, denoting the number of intervals. Each line of the Q subsequent lines contains two space-separated integers xi and yi, denoting the beginning and the end of ith interval.
Output Format
For each interval, print the count of the greatest letter occurring in the string in that interval.
My Algorithm
1. To lowercase the string
2. Loop through the queries array
3. Get the string from start interval to end interval
4. Find greatest char in the string
5. Count the occurrence, and add it to the array
6. Return the array
My Code
def getMaxCharCount(s, queries):
# queries is a n x 2 array where queries[i][0] and queries[i][1] represents x[i] and y[i] for the ith query.
maxCount = []
finalWord = s.lower()
for interval in queries:
if interval[0] == interval[1]:
maxCount.append(1)
else:
string = finalWord[interval[0]:interval[1]+1]
maxCount.append(string.count(max(string)))
return maxCount
Any help would be appreciated