# Maximum occurrence of prefix in the Array

• Last Updated : 28 Jan, 2022

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```

Approach: The idea is to observe that every prefix of the array must contain the first character of the string and each of its 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 string``    ``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`

## Javascript

 ``

Output:

`2`

My Personal Notes arrow_drop_up