Given a string ( containing characters from ‘0’ to ‘9’) and two digits and . The task is to find the substring in the given string with maximum occurrences and containing a’s and b’s only. If there are two or more such substrings with same frequencies then print the lexicographically smallest. If there does not exists any such substring then print -1.
Input : str = "47", a = 4, b = 7 Output : 4 Input : str = "23", a = 4, b = 7 Output : -1
The idea is to observe that we need to find the substring with maximum number of occurrences. So, if we consider substrings that contains both a’s and b’s then the number of occurrences will be less than if we consider the substrings with single digits ‘a’ and ‘b’ individually.
So, the idea is to calculate the frequency of digits of ‘a’ and ‘b’ in the string and the one with maximum frequency will be the answer.
Note: If both digits have same frequency then the digit which is lexicographically smaller among ‘a’ and ‘b’ will be the answer.
Below is the implementation of the above approach:
- Lexicographically smallest K-length substring containing maximum number of vowels
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Swap all occurrences of two characters to get lexicographically smallest string
- Lexicographically smallest and largest substring of size k
- Lexicographic smallest permutation of a String containing the second String as a Substring
- K-th lexicographically smallest unique substring of a given string
- Longest Subsequence of a String containing only vowels
- Longest Subsequence of a String containing only Consonants
- Count of Subsets containing only the given value K
- Count of substrings containing only the given character
- Count of substrings of a Binary string containing only 1s
- Shortest substring of a string containing all given words
- Count occurrences of a substring recursively
- Longest substring between any pair of occurrences ōf similar characters
- Count N-length strings consisting only of vowels sorted lexicographically
- Check if string can be made lexicographically smaller by reversing any substring
- Smallest string obtained by removing all occurrences of 01 and 11 from Binary String
- Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Lexicographically smallest string of length N and sum K
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.