Given a string S of size N and Q queries. Every query consists of L and R ( 0 < = L < = R < N ) . The task is to print the character that occurs the maximum number of times in the given range. If there are multiple characters that occur maximum number of times then print the lexicographically smallest of them.
Note: S consists of lowercase English letters.
str = “geekss”,
Q = 2
str = “striver”,
Q = 3
Naive Approach: A naive approach is to iterate from L to R for every query and find the character that appears the maximum number of times using frequency array of size 26.
Time Complexity: O(max(R-L), 26) per query.
Efficient Approach: An efficient approach is to use a prefix sum array to efficiently answer the query. Let pre[i][j] stores the occurence of a character j till the i-th index. For every query ocuurence of a character j will be pre[r][j] – pre[l-1][j](if l > 0). Find the lexicographically smallest character that appears the maximum number of times by iterating the 26 lowercase letters.
Below is the implementation of the above approach:
Query 1: s Query 2: r Query 3: e
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number of times the given string occurs in the array in the range [l, r]
- Lexicographically largest subsequence such that every character occurs at least k times
- Longest subsequence where each character occurs at least k times
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Replace every character of string by character whose ASCII value is K times more than it
- Queries for Nth smallest character for a given range in a string
- Print the string after the specified character has occurred given no. of times
- Queries to find total number of duplicate character in range L to R in the string S
- Queries to find Kth greatest character in a range [L, R] from a string with updates
- Number of sub-strings that contain the given character exactly k times
- Maximum length prefix of one string that occurs as subsequence in another
- Minimum swaps required to move all vowels occurs after consonants in a given string
- Count of substrings which contains a given character K times
- String Range Queries to find the number of subsets equal to a given String
- Queries to find the last non-repeating character in the sub-string of a given string
- Queries for rotation and Kth character of the given string in constant time
- Shortest distance to every other character from given character
- Replace all occurrences of character X with character Y in given string
- Check if the string contains consecutive letters and each letter occurs exactly once
- Longest subsequence where every character appears at-least k times
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.