Intersection of two String - Python
Last Updated :
09 Apr, 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.
Similar Reads
Python | Intersection of two lists The task of finding the intersection of two lists involves identifying the common elements between them. This means we want to extract the values that appear in both lists, while ignoring any duplicates or values that are unique to each list. For example, if we have two lists [4, 9, 1, 17, 11] and [
4 min read
Python Set - intersection_update() Method Python intersection_update() method is used to update a set with common elements only of all the sets passed in parameter of intersection_update() method. Python set intersection_update() Method Syntax: Syntax: set.intersection_update(set1,set2,set3,...........,set n) Parameters:Â One or more sets R
2 min read
Python | Intersection of two nested list Finding the intersection of two nested lists means figuring out which elements or sub-lists are present in both lists. The lists can have different structures and levels of complexity. In this article, we'll explore various methods to efficiently identify the common elements between two nested lists
3 min read
Python String Concatenation String concatenation in Python allows us to combine two or more strings into one. In this article, we will explore various methods for achieving this. The most simple way to concatenate strings in Python is by using the + operator.Using + OperatorUsing + operator allows us to concatenation or join s
3 min read
Python | Sympy Line.intersection() method In Sympy, the function intersection() is used to find the intersection with another geometrical entity. Syntax: Line.intersection(o) Parameters: o: Point or LinearEntity Returns: intersection: list of geometrical entities Example #1: Python3 1== # import sympy and Point, Line from sympy import Point
1 min read