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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find the number of occurrences
// of prefix which occurs maximum no. of time
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the count of the
// required prefix
int prefixOccurrences(string str)
{
    char c = str[0];
    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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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[0]; 
        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

chevron_right


Output:

2


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.