Sunday, April 27, 2025
Google search engine
HomeData Modelling & AIPython | Count all prefixes in given string with greatest frequency

Python | Count all prefixes in given string with greatest frequency

Given a string, print and count all prefixes in which first alphabet has greater frequency than second alphabet.
Take two alphabets from the user and compare them. The prefixes in which the alphabet given first has greater frequency than the second alphabet, such prefixes are printed, else the result will be 0. 

Examples : 

Input : string1 = "geek", 
        alphabet1 = "e", alphabet2 = "k"
Output :
ge
gee
geek
3

Input : string1 = "geek",
        alphabet1 = "k", alphabet2 = "e"
Output :
0

Approach: Take an empty string to store the string values of all the prefixes formed. Then check for the alphabet with greater frequency than the second alphabet. If no such case is found then the result will be 0 prefixes.

Implementation:

Python3




# Python program to Count all
# prefixes in given string with
# greatest frequency
 
# Function to print the prefixes
def prefix(string1, alphabet1, alphabet2):
    count = 0
    non_empty_string = ""
     
    string2 = list(string1)
     
    # Loop for iterating the length of
    # the string and print the prefixes
    # and the count of query prefixes.
    for i in range(0, len(string2)):
        non_empty_string = non_empty_string + (string2[i])
         
        if (non_empty_string.count(alphabet1) >
            non_empty_string.count(alphabet2)):
                 
            # prints all required prefixes
            print(non_empty_string)
             
            # increment count
            count += 1
             
    # returns count of the
    # required prefixes
    return(count)
     
# Driver Code
print(prefix("neveropen", "e", "g"))


Output

gee
geek
neveropen
neveropenf
neveropenfo
neveropenfor
neveropenforge
neveropenforgee
neveropenforgeek
neveropen
10

Complexity Analysis:

  • Time Complexity: O(N), where N is the length of the string.
  • Auxiliary Space: O(N)

Another approach that could be taken is to use a dictionary to store the frequencies of each character in the prefixes as they are generated. This way, rather than iterating through the prefix and counting the occurrences of the two characters every time, you can simply retrieve their frequencies from the dictionary. This can potentially improve the performance of the prefix function, especially for longer strings.

Here is an example of how this approach could be implemented:

Python3




def prefix(string1, alphabet1, alphabet2):
    count = 0
    non_empty_string = ""
    prefix_freqs = {}
      
    string2 = list(string1)
      
    # Loop for iterating the length of
    # the string and print the prefixes
    # and the count of query prefixes.
    for i in range(0, len(string2)):
        non_empty_string = non_empty_string + (string2[i])
        prefix_freqs[non_empty_string] = {}
        prefix_freqs[non_empty_string][alphabet1] = non_empty_string.count(alphabet1)
        prefix_freqs[non_empty_string][alphabet2] = non_empty_string.count(alphabet2)
          
        if (prefix_freqs[non_empty_string][alphabet1] > prefix_freqs[non_empty_string][alphabet2]):
            # prints all required prefixes
            print(non_empty_string)
              
            # increment count
            count += 1
              
    # returns count of the
    # required prefixes
    return(count)
   
# Driver Code
print(prefix("neveropen", "e", "g"))


Output

gee
geek
neveropen
neveropenf
neveropenfo
neveropenfor
neveropenforge
neveropenforgee
neveropenforgeek
neveropen
10

In the case of the prefix function, the time complexity is O(N), where N is the length of the string. This is because the function loops through the string once and performs a constant amount of work for each character in the string.

In the case of the prefix function, the auxiliary space used is O(N), where N is the length of the string. This is because the function stores the frequencies of all prefixes in a dictionary, and the size of the dictionary is directly proportional to the length of the string.

Using operator.countOf() method

Python3




import operator as op
def prefix(string1, alphabet1, alphabet2):
    count = 0
    non_empty_string = ""
    prefix_freqs = {}
      
    string2 = list(string1)
      
    # Loop for iterating the length of
    # the string and print the prefixes
    # and the count of query prefixes.
    for i in range(0, len(string2)):
        non_empty_string = non_empty_string + (string2[i])
        prefix_freqs[non_empty_string] = {}
        prefix_freqs[non_empty_string][alphabet1] = op.countOf(non_empty_string,alphabet1)
        prefix_freqs[non_empty_string][alphabet2] = op.countOf(non_empty_string,alphabet2)
          
        if (prefix_freqs[non_empty_string][alphabet1] > prefix_freqs[non_empty_string][alphabet2]):
            # prints all required prefixes
            print(non_empty_string)
              
            # increment count
            count += 1
              
    # returns count of the
    # required prefixes
    return(count)
   
# Driver Code
print(prefix("neveropen", "e", "g"))


Output

gee
geek
neveropen
neveropenf
neveropenfo
neveropenfor
neveropenforge
neveropenforgee
neveropenforgeek
neveropen
10

Time Complexity: O(N), 
Auxiliary Space: O(N)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments