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.
Below is the implementation :
# 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")) |
gee geek geeks geeksf geeksfo geeksfor geeksforge geeksforgee geeksforgeek geeksforgeeks 10
Recommended Posts:
- Python | Frequency of substring in given string
- Python | Frequency of each character in String
- Find frequency of each word in a string in Python
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Python String | count()
- Python | Count occurrences of a character in string
- Python | Count and display vowels in a string
- Python | Ways to count number of substring in string
- Python program to count number of vowels using sets in given string
- Python | Count the Number of matching characters in a pair of string
- Frequency of a substring in a string
- Remove even frequency characters from the string
- Frequency of a string in an array of strings
- Longest sub-string having frequency of each character less than equal to k
- Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)
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.



