Python Program to Check Overlapping Prefix - Suffix in Two Lists

Generally, a prefix is a sequence of characters that appear at the beginning of a string or list, and a suffix is a sequence of characters that appear at the end of a string or list.

For example, when we consider the list [1,2,3,4,5], a prefix could be [1,2,3] and a suffix could be [3,4,5]. Notice how they overlap at element 3.

Checking for overlapping prefixes and suffixes in two lists involves comparing elements to determine if the end of one list matches the beginning of another list. This concept is useful in string manipulation, text processing, and data analysis.

In this article, we'll explore different Python approaches to check overlapping prefix-suffix in two lists.

Understanding the Problem

We want to check if the suffix of the first list matches the prefix of the second list. For example ?

list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]

# Suffix of list1: [4, 5]
# Prefix of list2: [4, 5]
# They match, so there's an overlap

Method 1: Using Brute Force Approach

The brute force approach checks all possible prefix lengths to find a match. It compares the suffix of the first list with the prefix of the second list ?

Algorithm Steps

  • Iterate over all possible prefix lengths from 1 to the length of the shorter list

  • For each length, compare the suffix of list1 with the prefix of list2

  • If there is a match, return True

  • If no match is found, return False

Example

def has_overlapping_prefix_suffix(list1, list2):
    min_length = min(len(list1), len(list2))
    
    for length in range(1, min_length + 1):
        # Compare suffix of list1 with prefix of list2
        if list1[-length:] == list2[:length]:
            return True
    return False

# Test the function
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]

if has_overlapping_prefix_suffix(list1, list2):
    print("Lists have an overlapping prefix and suffix.")
else:
    print("Lists do not have an overlapping prefix and suffix.")

# Test with another example
list3 = [1, 2, 3]
list4 = [4, 5, 6]
print(f"Lists {list3} and {list4} overlap: {has_overlapping_prefix_suffix(list3, list4)}")
Lists have an overlapping prefix and suffix.
Lists [1, 2, 3] and [4, 5, 6] overlap: False

Method 2: Using String-Based Approach

This approach converts lists to strings and uses string methods for pattern matching ?

def has_overlapping_prefix_suffix_string(list1, list2):
    # Convert lists to strings with a delimiter
    str1 = ','.join(map(str, list1))
    str2 = ','.join(map(str, list2))
    
    min_length = min(len(list1), len(list2))
    
    for length in range(1, min_length + 1):
        suffix_str = ','.join(map(str, list1[-length:]))
        prefix_str = ','.join(map(str, list2[:length]))
        
        if suffix_str == prefix_str:
            return True
    return False

# Test the function
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]

result = has_overlapping_prefix_suffix_string(list1, list2)
print(f"String-based approach result: {result}")
String-based approach result: True

Method 3: Finding Maximum Overlap Length

This approach not only checks for overlap but also returns the length of the maximum overlap ?

def find_max_overlap_length(list1, list2):
    max_overlap = 0
    min_length = min(len(list1), len(list2))
    
    for length in range(1, min_length + 1):
        if list1[-length:] == list2[:length]:
            max_overlap = length
    
    return max_overlap

# Test the function
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]

overlap_length = find_max_overlap_length(list1, list2)
print(f"Maximum overlap length: {overlap_length}")

if overlap_length > 0:
    print(f"Overlapping elements: {list1[-overlap_length:]}")
else:
    print("No overlap found")
Maximum overlap length: 2
Overlapping elements: [4, 5]

Comparison of Methods

Method Time Complexity Space Complexity Best For
Brute Force O(n²) O(1) Simple boolean check
String-based O(n²) O(n) Complex data types
Max Overlap O(n²) O(1) Finding overlap details

Conclusion

Use the brute force approach for simple overlap detection. The max overlap method is useful when you need to know the exact overlap length. All methods have similar time complexity, so choose based on your specific requirements.

Updated on: 2026-03-27T15:50:14+05:30

512 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements