Python - Characters which Occur in More than K Strings
Last Updated :
26 Apr, 2023
Sometimes, while working with Python, we have a problem in which we compute how many characters are present in string. But sometimes, we can have a problem in which we need to get all characters that occur in atleast K Strings. Let's discuss certain ways in which this task can be performed.
Method #1: Using set() + Counter() + items() + loop The combination of above functions can be used to perform this particular task. In this, we find the count using Counter and set is used to limit the character duplicacy.
Python3
# Python3 code to demonstrate
# Characters which Occur in More than K Strings
# using set() + Counter() + loop + items()
from collections import Counter
from itertools import chain
def char_ex(strs, k):
temp = (set(sub) for sub in strs)
counts = Counter(chain.from_iterable(temp))
return {chr for chr, count in counts.items() if count >= k}
# Initializing list
test_list = ['Gfg', 'ise', 'for', 'Geeks']
# printing original list
print("The original list is : " + str(test_list))
# Initializing K
K = 2
# Characters which Occur in More than K Strings
# using set() + Counter() + loop + items()
res = char_ex(test_list, K)
# printing result
print ("Filtered Characters are : " + str(res))
Output : The original list is : ['Gfg', 'ise', 'for', 'Geeks']
Filtered Characters are : {'e', 'G', 's', 'f'}
Time complexity: O(nm) where n is the number of strings in the list and m is the average length of the strings.
Auxiliary Space complexity: O(nm) as well, as it uses a set and a Counter object to store characters and their counts.
Method #2: Using dictionary comprehension + set() + Counter() The combination of these functions perform this task in similar way as above. The difference is just that we use dictionary comprehension to give a compact solution to problem.
Python3
# Python3 code to demonstrate
# Characters which Occur in More than K Strings
# using set() + Counter() + dictionary comprehension
from collections import Counter
# Initializing list
test_list = ['Gfg', 'ise', 'for', 'Geeks']
# printing original list
print("The original list is : " + str(test_list))
# Initializing K
K = 2
# Characters which Occur in More than K Strings
# using set() + Counter() + dictionary comprehension
res = {key for key, val in Counter([ele for sub in
test_list for ele in set(sub)]).items()
if val >= K}
# printing result
print ("Filtered Characters are : " + str(res))
Output : The original list is : ['Gfg', 'ise', 'for', 'Geeks']
Filtered Characters are : {'e', 'G', 's', 'f'}
Time complexity: O(n), where n is the total number of characters in the test_list.
Auxiliary Space: O(n), as the Counter object stores the count of all characters in the test_list.
Method #3: Using nested for loops
Step-by-step approach:
- Join all the strings of test_list and store in x
- Remove duplicate characters from x and store again in x using list(),set() methods
- Count the occurrence each character of x in each string of test_list, and check if the count is greater than or equal to K
- If yes append to output list res
- Display res
Python3
# Python3 code to demonstrate
# Characters which Occur in More than K Strings
# Initializing list
test_list = ['Gfg', 'ise', 'for', 'Geeks']
# printing original list
print("The original list is : " + str(test_list))
# Initializing K
K = 2
res=[]
# Characters which Occur in More than K Strings
x="".join(test_list)
x=list(set(x))
for i in x:
c=0
for j in test_list:
if i in j:
c+=1
if(c>=K):
res.append(i)
# printing result
print ("Filtered Characters are : " + str(res))
OutputThe original list is : ['Gfg', 'ise', 'for', 'Geeks']
Filtered Characters are : ['G', 'f', 's', 'e']
Time Complexity: O(M*N) ,where M is the length of test_list N - length of each string in test_list
Auxiliary Space: O(N) ,where N is the length of res
Similar Reads
Python - Characters Index occurrences in String Sometimes, while working with Python Strings, we can have a problem in which we need to check for all the characters indices. The position where they occur. This kind of application can come in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using set() + reg
6 min read
String with Most Unique Characters - Python The task of finding the string with the most unique characters in a list involves determining which string contains the highest number of distinct characters. This is done by evaluating each string's distinct elements and comparing their counts. The string with the greatest count of unique character
4 min read
Count the number of characters in a String - Python The goal here is to count the number of characters in a string, which involves determining the total length of the string. For example, given a string like "GeeksForGeeks", we want to calculate how many characters it contains. Letâs explore different approaches to accomplish this.Using len()len() is
2 min read
Iterate over characters of a string in Python In this article, we will learn how to iterate over the characters of a string in Python. There are several methods to do this, but we will focus on the most efficient one. The simplest way is to use a loop. Letâs explore this approach.Using for loopThe simplest way to iterate over the characters in
2 min read
Python - N sized substrings with K distinct characters Given a String, the task is to write a Python program to extract strings of N size and have K distinct characters. Examples: Input : test_str = 'geeksforgeeksforgeeks', N = 3, K = 2 Output : ['gee', 'eek', 'gee', 'eek', 'gee', 'eek'] Explanation : 3 lengths have 2 unique characters are extracted. In
5 min read