Program to find length of subsequence that can be removed still t is subsequence of s in Python

Suppose we have a string s and another string t, where t is a subsequence of s. We need to find the maximum length of a substring that can be removed from s such that t remains a subsequence of the remaining string.

So, if the input is like s = "xyzxyxz" and t = "yz", then the output will be 4, as we can remove a substring of length 4 while keeping "yz" as a subsequence.

Approach

The algorithm works by considering three scenarios ?

  • Remove a suffix after matching all characters of t

  • Remove a prefix before matching all characters of t

  • Remove a substring between two parts of the matched subsequence

Algorithm Steps

To solve this, we follow these steps ?

  • Find positions where characters of t match in s from left to right

  • Find positions where characters of t match in s from right to left

  • Calculate the maximum removable length for each scenario

  • Return the maximum of all possible removable lengths

Example

class Solution:
    def solve(self, s, t):
        left = []
        right = []
        c1 = -1  # Remove suffix
        c2 = -1  # Remove prefix
        c3 = -1  # Remove middle substring
        
        # Find leftmost matching positions
        j = 0
        for i in range(len(s)):
            if s[i] == t[j]:
                left.append(i)
                j += 1
            if j == len(t):
                c1 = len(s) - i - 1  # Characters after last match
                break
        
        # Find rightmost matching positions
        j = len(t) - 1
        for i in range(len(s) - 1, -1, -1):
            if s[i] == t[j]:
                right.insert(0, i)
                j -= 1
            if j == -1:
                c2 = i  # Characters before first match
                break
        
        # Find maximum gap between consecutive matches
        for i in range(len(t) - 1):
            c3 = max(c3, right[i + 1] - left[i] - 1)
        
        return max(c1, c2, c3)

# Test the solution
ob = Solution()
s = "xyzxyxz"
t = "yz"
result = ob.solve(s, t)
print(f"Maximum removable length: {result}")
Maximum removable length: 4

How It Works

For the example s = "xyzxyxz" and t = "yz" ?

  • Left matching: Characters 'y' and 'z' are found at positions [2, 6]

  • Right matching: Characters 'y' and 'z' are found at positions [4, 6]

  • Scenario 1 (suffix): After matching "yz" ending at position 6, we can remove 0 characters

  • Scenario 2 (prefix): Before matching "yz" starting at position 4, we can remove 4 characters

  • Scenario 3 (middle): Between positions 2 and 6, we can remove 6-2-1 = 3 characters

The maximum is 4, achieved by removing the prefix "xyzx".

Conclusion

This algorithm efficiently finds the maximum removable substring length by considering all possible removal scenarios. It uses a two-pointer approach to identify optimal matching positions from both directions.

Updated on: 2026-03-25T13:43:02+05:30

271 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements