# Maximum occurrence of prefix in the Array

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.

Examples:

```Input : str = "abbcdabbcd"
Output :  2
The prefix "abb" has maximum number of
occurrences 2.

Input : str = "abc"
Output :  1
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

## C++

 `// CPP program to find the number of occurrences ` `// of prefix which occurs maximum no. of time ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to return the count of the ` `// required prefix ` `int` `prefixOccurrences(string str) ` `{ ` `    ``char` `c = str; ` `    ``int` `countc = 0; ` ` `  `    ``// Find the frequency of first ` `    ``// character of string ` `    ``for` `(``int` `i = 0; i < str.length(); i++) { ` `        ``if` `(str[i] == c) ` `            ``countc++; ` `    ``} ` ` `  `    ``return` `countc; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"abbcdabbcd"``; ` `    ``cout << prefixOccurrences(str); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find the number  ` `// of occurrences of prefix which ` `// occurs maximum no. of time  ` `class` `GFG ` `{ ` `     `  `// Function to return the count of the  ` `// required prefix  ` `static` `int` `prefixOccurrences(String str)  ` `{  ` `    ``char` `c = str.charAt(``0``);  ` `    ``int` `countc = ``0``;  ` ` `  `    ``// Find the frequency of first  ` `    ``// character of string  ` `    ``for` `(``int` `i = ``0``; i < str.length(); i++) ` `    ``{  ` `        ``if` `(str.charAt(i) == c)  ` `            ``countc++;  ` `    ``}  ` ` `  `    ``return` `countc;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main(String args[]) ` `{  ` `    ``String str = ``"abbcdabbcd"``;  ` `    ``System.out.println(prefixOccurrences(str));  ` ` `  `} ` `}  ` ` `  `// This code is contributed by Arnab Kundu `

## Python3

 `# Python3 program to find the number  ` `# of occurrences of prefix which ` `# occurs maximum no. of time ` ` `  `# Function to return the count ` `# of the required prefix ` `def` `prefixOccurrences(str1): ` ` `  `    ``c ``=` `str1[``0``] ` `    ``countc ``=` `0` ` `  `    ``# Find the frequency of first ` `    ``# character of str1ing ` `    ``for` `i ``in` `range``(``len``(str1)): ` `        ``if` `(str1[i] ``=``=` `c): ` `            ``countc ``+``=` `1` ` `  `    ``return` `countc ` ` `  `# Driver code ` `str1 ``=` `"abbcdabbcd"` `print``(prefixOccurrences(str1)) ` ` `  `# This code is contributed ` `# by mohit kumar `

## C#

 `// C# program to find the number  ` `// of occurrences of prefix which  ` `// occurs maximum no. of time  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` `     `  `    ``// Function to return the count of the  ` `    ``// required prefix  ` `    ``static` `int` `prefixOccurrences(``string` `str)  ` `    ``{  ` `        ``char` `c = str;  ` `        ``int` `countc = 0;  ` `     `  `        ``// Find the frequency of first  ` `        ``// character of string  ` `        ``for` `(``int` `i = 0; i < str.Length; i++)  ` `        ``{  ` `            ``if` `(str[i] == c)  ` `                ``countc++;  ` `        ``}  ` `     `  `        ``return` `countc;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``string` `str = ``"abbcdabbcd"``;  ` `         `  `        ``Console.WriteLine(prefixOccurrences(str));  ` `    ``}  ` `}  ` ` `  `// This code is contributed by Ryuga `

Output:

```2
```

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.