Skip to content
Related Articles

Related Articles

Python | Count all prefixes in given string with greatest frequency

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 19 Aug, 2022
View Discussion
Improve Article
Save Article

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("geeksforgeeks", "e", "g"))


Output

gee
geek
geeks
geeksf
geeksfo
geeksfor
geeksforge
geeksforgee
geeksforgeek
geeksforgeeks
10

Complexity Analysis:

  • Time Complexity: O(N), where N is the length of the string.
  • Auxiliary Space: O(N)
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!