Given a string consisting of lower case English Alphabets. The task is to count the number of occurrences of the non-empty prefix which occurs the maximum number of times in the string as a substring.
Input : str = "abbcdabbcd" Output : 2 The prefix "abb" has maximum number of occurrences 2. Input : str = "abc" Output : 1
Approach: The idea is to observe that every prefix of the array must contain the first character of the string and each of it’s respective occurrences will also. Also the 1st character of a string is the minimum length prefix. So, the prefix with maximum number of occurrences will be the first character of the string itself. Therefore, the task now reduces to find the frequency of the first character in the given string.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Find the maximum length of the prefix
- Maximum length prefix of one string that occurs as subsequence in another
- Count all elements in the array which appears at least K times after their first occurrence
- Sort an array in descending order based on the sum of its occurrence
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Strings from an array which are not prefix of any other string
- Index with Minimum sum of prefix and suffix sums in an Array
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Longest string in an array which matches with prefix of the given string
- Maximum subarray sum in array formed by repeating the given array k times
- Maximum value of arr[i] % arr[j] for a given array
- Find the Nth occurrence of a character in the given String
- Find indices of all occurrence of one string in other
- Find the maximum subarray XOR in a given array
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.