Python - All substrings Frequency in String
Last Updated :
16 May, 2023
Given a String, extract all unique substrings with their frequency.
Input : test_str = "ababa"
Output : {'a': 3, 'ab': 2, 'aba': 2, 'abab': 1, 'ababa': 1, 'b': 2, 'ba': 2, 'bab': 1, 'baba': 1}
Explanation : All substrings with their frequency extracted.
Input : test_str = "GFGF"
Output : {'G': 2, 'GF': 2, 'GFG': 1, 'GFGF': 1, 'F': 2, 'FG': 1, 'FGF': 1}
Explanation : All substrings with their frequency extracted.
Method #1: Using count() method
First, we need to find all the substrings then count() method can be used to find the frequency of a substring and store it in the dictionary. Then, simply print the dictionary.
Python3
# Python3 code to demonstrate working of
# All substrings Frequency in String
# Using loop + list comprehension
# initializing string
test_str = "abababa"
# printing original string
print("The original string is : " + str(test_str))
# list comprehension to extract substrings
temp = [test_str[idx: j] for idx in range(len(test_str)) for j in range(idx + 1, len(test_str) + 1)]
# loop to extract final result of frequencies
d=dict()
for i in temp:
d[i]=test_str.count(i)
# printing result
print("Extracted frequency dictionary : " + str(d))
OutputThe original string is : abababa
Extracted frequency dictionary : {'a': 4, 'ab': 3, 'aba': 2, 'abab': 1, 'ababa': 1, 'ababab': 1, 'abababa': 1, 'b': 3, 'ba': 3, 'bab': 1, 'baba': 1, 'babab': 1, 'bababa': 1}
Method #2: Using loop + list comprehension
The combination of the above functionalities can be used to solve this problem. In this, we first extract all the substrings using list comprehension, post that loop is used to increase frequency.
Python3
# Python3 code to demonstrate working of
# All substrings Frequency in String
# Using loop + list comprehension
# initializing string
test_str = "abababa"
# printing original string
print("The original string is : " + str(test_str))
# list comprehension to extract substrings
temp = [test_str[idx: j] for idx in range(len(test_str))
for j in range(idx + 1, len(test_str) + 1)]
# loop to extract final result of frequencies
res = {}
for idx in temp:
if idx not in res.keys():
res[idx] = 1
else:
res[idx] += 1
# printing result
print("Extracted frequency dictionary : " + str(res))
OutputThe original string is : abababa
Extracted frequency dictionary : {'a': 4, 'ab': 3, 'aba': 3, 'abab': 2, 'ababa': 2, 'ababab': 1, 'abababa': 1, 'b': 3, 'ba': 3, 'bab': 2, 'baba': 2, 'babab': 1, 'bababa': 1}
Method #3: Using list comprehension
This is yet another way in which this task can be performed. In this, we perform both the tasks, of extracting substring and computing frequency in a single nested list comprehension.
Python3
# Python3 code to demonstrate working of
# All substrings Frequency in String
# Using list comprehension
# initializing string
test_str = "abababa"
# printing original string
print("The original string is : " + str(test_str))
# list comprehension to extract substrings and frequency
res = dict()
for ele in [test_str[idx: j] for idx in range(len(test_str)) for j in range(idx + 1, len(test_str) + 1)]:
res[ele] = 1 if ele not in res.keys() else res[ele] + 1
# printing result
print("Extracted frequency dictionary : " + str(res))
OutputThe original string is : abababa
Extracted frequency dictionary : {'a': 4, 'ab': 3, 'aba': 3, 'abab': 2, 'ababa': 2, 'ababab': 1, 'abababa': 1, 'b': 3, 'ba': 3, 'bab': 2, 'baba': 2, 'babab': 1, 'bababa': 1}
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method #4: Using regex + findall() method
Step by step Algorithm:
- Initialize a dictionary 'd' to store substring frequencies.
- Loop through range(1, len(test_str)+1).
- For each i in range, find all substrings of length i using regex findall function.
- For each substring 'sub', update its frequency in the dictionary 'd'.
- Return the 'd' dictionary with substring frequencies.
Python3
import re
# initializing string
test_str = "abababa"
# printing original string
print("The original string is : " + str(test_str))
# using regex to count substring frequencies
d = {}
for i in range(1, len(test_str)+1):
for sub in re.findall('(?=(.{'+str(i)+'}))', test_str):
d[sub] = d.get(sub, 0) + 1
# printing result
print("Extracted frequency dictionary : " + str(d))
OutputThe original string is : abababa
Extracted frequency dictionary : {'a': 4, 'b': 3, 'ab': 3, 'ba': 3, 'aba': 3, 'bab': 2, 'abab': 2, 'baba': 2, 'ababa': 2, 'babab': 1, 'ababab': 1, 'bababa': 1, 'abababa': 1}
Time complexity: O(n^2), where n is the length of the input string. The nested loops for finding substrings and counting their frequencies contribute to the O(n^2) time complexity.
Auxiliary Space: O(n), where n is the length of the input string.
Method 5: Using a sliding window technique with a dictionary to keep track of the counts.
Step-by-step approach:
- Initialize an empty dictionary freq_dict to keep track of the substring frequencies.
- Initialize a variable n to the length of the given string test_str.
- Loop through the range of n:
- Initialize a variable window_size to i + 1.
- Loop through the range n - window_size + 1:
- Initialize a variable substring to the substring from test_str starting at the current index and having length window_size.
- If substring is already in freq_dict, increment its value by 1. Otherwise, add it to freq_dict with a value of 1.
- Return the freq_dict.
Python3
test_str = "abababa"
print("The original string is : " + str(test_str))
# using sliding window with a dictionary to count substring frequencies
freq_dict = {}
n = len(test_str)
for i in range(n):
window_size = i + 1
for j in range(n - window_size + 1):
substring = test_str[j:j+window_size]
freq_dict[substring] = freq_dict.get(substring, 0) + 1
# printing result
print("Extracted frequency dictionary : " + str(freq_dict))
OutputThe original string is : abababa
Extracted frequency dictionary : {'a': 4, 'b': 3, 'ab': 3, 'ba': 3, 'aba': 3, 'bab': 2, 'abab': 2, 'baba': 2, 'ababa': 2, 'babab': 1, 'ababab': 1, 'bababa': 1, 'abababa': 1}
Time complexity: O(n^3), since we have a nested loop over the range of n and over the range n - window_size + 1 for each window_size.
Auxiliary space: O(n^3), since we are storing all possible substrings in the dictionary.
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read