Intersection of two String - Python
Last Updated :
11 Jul, 2025
We are given two strings and our task is to find the intersection of characters between them. This means we need to identify which characters are common in both strings, regardless of their position. For example, if we have strings like "GeeksforGeeks" and "Codefreaks", the common characters would be those that appear in both, such as 'e', 'k', 's', etc. Let’s explore some efficient ways to perform this intersection.
Using set intersection
By converting both strings into sets, we can quickly identify the common characters using either the & operator or the .intersection() method. This works great because sets automatically remove duplicates and allow for fast lookups. It’s clean, concise and performs well even with large strings. Example:
Python
a = 'GeeksforGeeks'
b = 'Codefreaks'
res = set(a) & set(b)
print(res)
Output{'f', 'o', 'k', 's', 'e', 'r'}
Explanation: & operator between set(a) and set(b) returns a set of characters that are common to both strings, effectively finding their intersection.
Using set() with list comprehension
This method combines the speed of sets with the flexibility of list comprehension. We convert one string to a set for quick character checking, then loop through the other string to pick out matching characters. It's helpful when we want to keep the order of characters from one of the strings. Example:
Python
a = 'GeeksforGeeks'
b = 'Codefreaks'
set_b = set(b)
res = [char for char in a if char in set_b]
print(set(res))
Output{'s', 'k', 'o', 'f', 'r', 'e'}
Explanation: set is created from string b for fast lookup and then a list comprehension checks each character in string a to see if it exists in that set, collecting the common characters.
Using counter
collections.Counter counts the frequency of each character in both strings, then finds their common elements with respect to their count. This is useful when dealing with strings where repetition matters, although it uses a bit more memory compared to sets. Example:
Python
from collections import Counter
a = 'GeeksforGeeks'
b = 'Codefreaks'
counter_a = Counter(a)
counter_b = Counter(b)
res = counter_a & counter_b
print(set(res.elements()))
Output{'k', 'o', 'f', 'r', 's', 'e'}
Explanation: & operator between the Counters gives common characters with their minimum counts and set(res.elements()) extracts the unique intersecting characters.
Using dictionary
This method involves creating a dictionary from one string’s characters, then checking each character in the second string against it. It mimics the behavior of sets but with more manual control. It’s slightly less efficient and a bit longer to write compared to using sets directly, but still maintains good performance due to fast dictionary lookups. Example:
Python
a = 'GeeksforGeeks'
b = 'Codefreaks'
dict_a = {char: True for char in a}
res = set(char for char in b if char in dict_a)
print(res)
Output{'e', 's', 'f', 'k', 'r', 'o'}
Explanation: A dictionary is created from string a where each character becomes a key. Then, for each character in string b, we check if it exists in that dictionary. If it does, it means the character is common to both strings, so we add it to a set to keep only unique common characters.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice