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:
- 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
- Index with Minimum sum of prefix and suffix sums in an Array
- 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
- Find indices of all occurrence of one string in other
- Program to count occurrence of a given character in a string
- Print characters having odd frequencies in order of occurrence
- Print characters and their frequencies in order of occurrence
- Count occurrence of a given character in a string using Stream API in Java
- Minimize the length of string by removing occurrence of only one character
- Maximum value of arr[i] % arr[j] for a given array
- Generate two output strings depending upon occurrence of character in input string.
- 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 email@example.com. 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.