Sometimes, while working with Python strings, we can have problem in which we need to extract the characters which have occurrences in more than one string in string list. This kind of problem usually occurs in web development and Data Science domains. Lets discuss certain ways in which this task can be performed.
Method #1 : Using Counter() + set() This is one of the ways in which this task can be performed. In this, we check for all the strings and compute the character frequency in each string and return the character which have more than one string occurrence.
Python3
# Python3 code to demonstrate working of # Characters occurring in multiple Strings # Using Counter() + set() from itertools import chain from collections import Counter # initializing list test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'Lazyroar' , 'and' , 'cs' ] # printing original list print ("The original list is : " + str (test_list)) # Characters occurring in multiple Strings # Using Counter() + set() temp = ( set (sub) for sub in test_list) cntr = Counter(chain.from_iterable(temp)) res = [ chr for chr , count in cntr.items() if count > = 2 ] # printing result print ("Characters with Multiple String occurrence : " + str (res)) |
The original list is : [‘gfg’, ‘is’, ‘best’, ‘for’, ‘Lazyroar’, ‘and’, ‘cs’] Characters with Multiple String occurrence : [‘g’, ‘e’, ‘f’, ‘s’]
Time Complexity: O(n), where n is the length of the input list. This is because we’re using the Counter() + set() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #2 : Using list comprehension + Counter() + set() This is one of the way in which this task can be performed. In this, we perform similar task as above, the difference is that we perform in compact and one-liner way using list comprehension.
Python3
# Python3 code to demonstrate working of # Characters occurring in multiple Strings # Using list comprehension + Counter() + set() from itertools import chain from collections import Counter # initializing list test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'Lazyroar' , 'and' , 'cs' ] # printing original list print ("The original list is : " + str (test_list)) # Characters occurring in multiple Strings # Using list comprehension + Counter() + set() res = [key for key, val in Counter([ele for sub in test_list for ele in set (sub)]).items() if val > 1 ] # printing result print ("Characters with Multiple String occurrence : " + str (res)) |
The original list is : [‘gfg’, ‘is’, ‘best’, ‘for’, ‘Lazyroar’, ‘and’, ‘cs’] Characters with Multiple String occurrence : [‘g’, ‘e’, ‘f’, ‘s’]
Time complexity: O(N*M), where N is length of test_list and M is number of characters of string in test_list
Auxiliary Space: O(K), where K is length of res list.
Method #3 : Using dict+ for loop().
In this code, we first create an empty dictionary called char_counts to store the character counts. We then iterate over each string in the list and, for each character in the string, either increment the count for the character in the dictionary or add the character to the dictionary with a count of 1.
After counting the characters in all the strings, we iterate over the dictionary and add any character that occurs in more than one string to the result set.
At the end of the loop, the result set will contain the characters that occur in multiple strings. then convert the set into list and We then print out the result using the print() function.
Python3
# Sample list of strings strings_list = [ 'gfg' , 'is' , 'best' , 'for' , 'Lazyroar' , 'and' , 'cs' ] # Create a dictionary to store the character counts char_counts = {} l = [] # Iterate over each string in the list for s in strings_list: # Iterate over each character in the string for c in s: # If the character is already in the dictionary, increment its count if c in char_counts: char_counts + = 1 # Otherwise, add the character to the dictionary with a count of 1 else : char_counts = 1 # Find the characters that occur in multiple strings result = set () for c, count in char_counts.items(): if count > 1 : result.add(c) #convert set to list z = list (result) # Print the characters with multiple string occurrence print ( 'Characters with Multiple String occurrence:' , z) |
Characters with Multiple String occurrence: ['e', 'g', 'f', 's']
Time complexity: O(N*M)
Auxiliary Space: O(K)
Method #4 : Using list(),set(),count() methods
Approach
- Concatenated all the strings using for loop and += operator
- Created a list with all unique characters of concatenated string(using list(),set() methods)
- Now check whether the each character of newly created list is occurring more than once using count()
- If occurs more than once append such characters to output list
- Display output list
Python3
# Python3 code to demonstrate working of # Characters occurring in multiple Strings # initializing list test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'Lazyroar' , 'and' , 'cs' ] # printing original list print ( "The original list is : " + str (test_list)) # Characters occurring in multiple Strings res = [] x = "" for i in test_list: x + = i y = list ( set (x)) for i in y: if x.count(i)> 1 : res.append(i) # printing result print ( "Characters with Multiple String occurrence : " + str (res)) |
The original list is : ['gfg', 'is', 'best', 'for', 'Lazyroar', 'and', 'cs'] Characters with Multiple String occurrence : ['s', 'g', 'e', 'f']
Time Complexity: O(N*N)
Auxiliary Space : O(N)