Python program to find the frequency of the elements which are common in a list of strings
Last Updated :
21 Mar, 2023
Given list of strings. The task is to find the frequency of the elements which are common in all strings given in the list of strings.
Examples:
Input : test_list = ["gegek", "gfgk", "kingg"]
Output : {'g': 2, 'k': 1}
Explanation : g occurs twice in all Strings.
Input : test_list = ["gefgek", "gfgk", "kinfgg"]
Output : {'g': 2, 'k': 1, 'f' : 1}
Explanation : f occurs once in all Strings.
Method : Using reduce() + lambda + Counter()
The combination of above functions can be used to solve this problem. In this, we perform the key role of counting using Counter() and lambda coupled with reduce() is used to perform intersection and extension of logic to all the strings respectively.
Python3
from functools import reduce
from collections import Counter
test_list = [ "geek" , "gfgk" , "king" ]
print ( "The original list is : " + str (test_list))
res = reduce ( lambda a, b: a & b, (Counter(ele) for ele in test_list[ 1 :]),
Counter(test_list[ 0 ]))
print ( "String intersection and frequency : " + str ( dict (res)))
|
Output
The original list is : ['geek', 'gfgk', 'king']
String intersection and frequency : {'g': 1, 'k': 1}
Time Complexity: O(n)
Space Complexity: O(n)
Approach #2: Using set and Counter objects
Step by step Algorithm:
- Initialize the input list “test_list”.
- Initialize an empty set “res_set” and add the first string from “test_list” to it.
- For each string “s” in “test_list[1:]”:
a. Find the intersection of “res_set” and set(s).
b. Update “res_set” with the intersection result.
- Initialize an empty Counter object “res_cnt”.
- For each character “char” in “res_set”:
a. Find the minimum count of “char” in all strings of “test_list”.
b. Add the key-value pair (char: count) to “res_cnt”.
- Print the dictionary of the final result.
Python3
from collections import Counter
test_list = [ "geek" , "gfgk" , "king" ]
print ( "The original list is : " + str (test_list))
res_set = set (test_list[ 0 ])
for s in test_list[ 1 :]:
res_set & = set (s)
res_cnt = Counter({char: min (s.count(char) for s in test_list) for char in res_set})
print ( "String intersection and frequency : " + str ( dict (res_cnt)))
|
Output
The original list is : ['geek', 'gfgk', 'king']
String intersection and frequency : {'k': 1, 'g': 1}
Time Complexity: O(nm^2). where n is the number of strings in the input list and m is the maximum length of a string. The set intersection operation takes O(m) time, and iterating over all n strings takes O(nm) time. The count operation on each string takes O(m) time. Therefore, the overall time complexity is O(nm^2).
Space Complexity: The space used by “res_set” is at most O(m), and the space used by “res_cnt” is at most O(len(res_set)). Therefore, the overall space complexity is O(m + len(res_set)).
Share your thoughts in the comments
Please Login to comment...