Find the Intersection of Two Strings

problem-solving
Published

April 30, 2024

Finding the common elements between two strings is a fundamental task in string manipulation. This blog post will explore several efficient methods to find the intersection of two strings in Python, catering to different levels of experience and performance needs.

Method 1: Using Sets

Leveraging Python’s built-in set data structure provides a highly efficient and elegant solution. Sets are unordered collections of unique elements, making them ideal for finding intersections. The & operator directly computes the intersection of two sets.

def intersection_using_sets(str1, str2):
  """Finds the intersection of two strings using sets.

  Args:
    str1: The first string.
    str2: The second string.

  Returns:
    A string containing the common characters, sorted alphabetically.  Returns an empty string if no common characters are found.
  """
  set1 = set(str1)
  set2 = set(str2)
  common_chars = set1 & set2
  return "".join(sorted(common_chars))

string1 = "hello"
string2 = "world"
result = intersection_using_sets(string1, string2)
print(f"The intersection of '{string1}' and '{string2}' is: {result}") # Output: lo

string3 = "python"
string4 = "programming"
result = intersection_using_sets(string3, string4)
print(f"The intersection of '{string3}' and '{string4}' is: {result}") #Output: gnopr

This method boasts excellent time complexity, typically O(min(m, n)), where ‘m’ and ‘n’ are the lengths of the strings.

Method 2: Using Loops

For a more explicit and beginner-friendly approach, we can use nested loops. This method iterates through each character of the first string and checks if it exists in the second string.

def intersection_using_loops(str1, str2):
  """Finds the intersection of two strings using nested loops.

  Args:
    str1: The first string.
    str2: The second string.

  Returns:
    A string containing the common characters, sorted alphabetically. Returns an empty string if no common characters are found.
  """
  common_chars = ""
  for char1 in str1:
    if char1 in str2 and char1 not in common_chars:
      common_chars += char1
  return "".join(sorted(common_chars))

string1 = "hello"
string2 = "world"
result = intersection_using_loops(string1, string2)
print(f"The intersection of '{string1}' and '{string2}' is: {result}") # Output: lo

While functional, this method has a time complexity of O(m*n), making it less efficient than the set-based approach for larger strings.

Method 3: List Comprehension (More Concise Looping)

List comprehension offers a more compact way to achieve the same result as the nested loop method:

def intersection_using_comprehension(str1, str2):
    """Finds the intersection using list comprehension."""
    common_chars = sorted(list(set([char for char in str1 if char in str2])))
    return "".join(common_chars)

#Example Usage
string1 = "hello"
string2 = "world"
result = intersection_using_comprehension(string1, string2)
print(f"The intersection of '{string1}' and '{string2}' is: {result}") # Output: lo

This method is functionally similar to the set method in terms of efficiency, but more concise.

Choosing the Right Method

For optimal performance, especially with larger strings, the set-based approach (Method 1) is strongly recommended. The loop-based methods (Method 2 and 3) are useful for understanding the underlying logic but are less efficient. The list comprehension approach provides a balance between conciseness and readability, while still maintaining relatively good performance. The choice depends on your priorities – speed, readability, or a balance of both.