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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find the maximum length of the prefix
- Maximum length prefix of one string that occurs as subsequence in another
- Sort an array in descending order based on the sum of its occurrence
- Count all elements in the array which appears at least K times after their first occurrence
- Strings from an array which are not prefix of any other string
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- 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
- Print characters having odd frequencies in order of occurrence
- Program to count occurrence of a given character in a string
- Print characters having even frequencies in order of occurrence
- Print characters and their frequencies in order of occurrence
- Find the maximum subarray XOR in a given array
- Longest Sub-array with maximum average value
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.