Python – Substring Suffix Frequency
Given a String and substring, count all the substitutes from string that can be used to complete the substring.
Input : test_str = “Gfg is good . Gfg is good . Gfg is better . Gfg is good .”, substr = “Gfg is”
Output : {‘good’: 3, ‘better’: 1}
Explanation : good occurs 3 times as suffix after substring in string hence 3. and so on.
Input : test_str = “Gfg is good . Gfg is good . Gfg is good . Gfg is good .”, substr = “Gfg is”
Output : {‘good’: 4}
Explanation : good occurs 4 times as suffix after substring in string hence 4. and so on.
Method #1 : Using regex() + defaultdict() + loop
This is one of the ways in which this task can be performed. In this, we construct regex for getting all the matching elements for substring. Then check all possible occurrences in String, frequency count using defaultdict().
Python3
from collections import defaultdict
import re
test_str = "Gfg is good . Gfg is best . Gfg is better . Gfg is good ."
print ( "The original string is : " + str (test_str))
substr = "Gfg is"
temp = re.findall(substr + " (\w+)" , test_str, flags = re.IGNORECASE)
res = defaultdict( int )
for idx in temp:
res[idx] + = 1
print ( "Frequency of replacements : " + str ( dict (res)))
|
Output
The original string is : Gfg is good . Gfg is best . Gfg is better . Gfg is good .
Frequency of replacements : {'good': 2, 'best': 1, 'better': 1}
Method #2 : Using Counter() + regex()
This is yet another way in which this task can be performed. In this, we compute elements frequency using Counter().
Python3
import re
from collections import Counter
test_str = "Gfg is good . Gfg is best . Gfg is better . Gfg is good ."
print ( "The original string is : " + str (test_str))
substr = "Gfg is"
temp = re.findall(substr + " (\w+)" , test_str, flags = re.IGNORECASE)
res = dict (Counter(temp))
print ( "Frequency of replacements : " + str (res))
|
Output
The original string is : Gfg is good . Gfg is best . Gfg is better . Gfg is good .
Frequency of replacements : {'good': 2, 'best': 1, 'better': 1}
Method #3 : Using split(),find(),count(),strip() methods
Python3
test_str = "Gfg is good . Gfg is good . Gfg is better . Gfg is good ."
print ( "The original string is : " + str (test_str))
substr = "Gfg is"
x = test_str.split( "." )
y = []
for i in x:
if (i.find(substr)! = - 1 ):
i = i.strip().split( " " )
y.append(i[ - 1 ])
y1 = list ( set (y))
d = dict ()
for i in y1:
d[i] = y.count(i)
print ( "Frequency of replacements : " + str (d))
|
Output
The original string is : Gfg is good . Gfg is good . Gfg is better . Gfg is good .
Frequency of replacements : {'good': 3, 'better': 1}
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Method #4 : Using operator.countOf() methods
Python3
import operator as op
test_str = "Gfg is good . Gfg is good . Gfg is better . Gfg is good ."
print ( "The original string is : " + str (test_str))
substr = "Gfg is"
x = test_str.split( "." )
y = []
for i in x:
if (i.find(substr)! = - 1 ):
i = i.strip().split( " " )
y.append(i[ - 1 ])
y1 = list ( set (y))
d = dict ()
for i in y1:
d[i] = op.countOf(y,i)
print ( "Frequency of replacements : " + str (d))
|
Output
The original string is : Gfg is good . Gfg is good . Gfg is better . Gfg is good .
Frequency of replacements : {'better': 1, 'good': 3}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using list comprehension and dictionary comprehension
Step by step approach:
- Split the original string into a list of sentences using the split() method and the period character as the separator.
- Filter out the non-matching sentences using a list comprehension that checks if the substring is present in each sentence using the in keyword.
- Strip the whitespace from each sentence using the strip() method.
- Extract the last word of each matching sentence using a list comprehension that splits each sentence into words using the split() method and gets the last element using the -1 index.
- Create a set of the unique last words using the set() function.
- Count the frequency of each last word using a dictionary comprehension that uses the count() method of the list of last words to count the occurrences of each unique word.
- Print the result.
Python3
test_str = "Gfg is good . Gfg is good . Gfg is better . Gfg is good ."
substr = "Gfg is"
sentences = [s.strip() for s in test_str.split( '.' ) if substr in s]
last_words = [s.split()[ - 1 ] for s in sentences]
freq = {w: last_words.count(w) for w in set (last_words)}
print ( "Frequency of replacements : " + str (freq))
|
Output
Frequency of replacements : {'good': 3, 'better': 1}
The time complexity of this approach is O(n), where n is the number of characters in the input string.
The space complexity of this approach is O(m), where m is the number of unique last words in the input string.
Last Updated :
23 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...