Python – Substring Suffix Frequency

Given a String and substring, count all the substitutes from string which 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 with occurrences in String, frequency count using defaultdict().



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Substring substitutes frequency
# Using regex() + defaultdict() + loop
from collections import defaultdict
import re
  
# initializing string
test_str = "Gfg is good . Gfg is best . Gfg is better . Gfg is good ."
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing substring
substr = "Gfg is"
  
# initializing regex
temp = re.findall(substr + " (\w+)", test_str, flags = re.IGNORECASE)
  
# adding values to form frequencies
res = defaultdict(int)
for idx in temp:
   res[idx] += 1
  
# printing result
print("Frequency of replacements : " + str(dict(res)))

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Substring substitutes frequency
# Using Counter() + regex()
import re
from collections import Counter
  
# initializing string
test_str = "Gfg is good . Gfg is best . Gfg is better . Gfg is good ."
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing substring
substr = "Gfg is"
  
# initializing regex
temp = re.findall(substr + " (\w+)", test_str, flags = re.IGNORECASE)
  
# adding values to form frequencies
res = dict(Counter(temp))
  
# printing result
print("Frequency of replacements : " + str(res))

chevron_right


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}



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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.