Open In App

Python program to find the frequency of the elements which are common in a list of strings

Last Updated : 21 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Strings list intersection
# Using reduce() + lambda + Counter()
from functools import reduce
from collections import Counter
 
# initializing list
test_list = ["geek", "gfgk", "king"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using & operator to perform intersection
res = reduce(lambda a, b: a & b, (Counter(ele) for ele in test_list[1:]),
             Counter(test_list[0]))
 
# printing result
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: 

  1. Initialize the input list “test_list”.
  2. Initialize an empty set “res_set” and add the first string from “test_list” to it.
  3. 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.
  4. Initialize an empty Counter object “res_cnt”.
  5. 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”.
  6. Print the dictionary of the final result.

Python3




from collections import Counter
# initializing list
test_list = ["geek", "gfgk", "king"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using set and Counter objects to find common characters
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})
 
# printing result
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)).



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads