Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find length of shortest supersequence in Python
A supersequence is a string that contains both input strings as subsequences. The shortest supersequence problem finds the minimum length string that includes all characters from both strings while preserving their order.
For example, if s = "pipe" and t = "people", then "pieople" is a supersequence with length 7.
Algorithm Approach
The solution uses dynamic programming to find the Longest Common Subsequence (LCS), then calculates the shortest supersequence length using the formula:
Shortest Supersequence Length = len(s) + len(t) - LCS_length
Step-by-Step Process
Create a 2D table to store LCS lengths
Fill the table using dynamic programming
If characters match, add 1 to diagonal value
If characters don't match, take maximum from left or top
Return total length minus LCS length
Example
Let us see the implementation to find the shortest supersequence length ?
class Solution:
def solve(self, s, t):
m = len(s)
n = len(t)
table = [[0 for i in range(n + 1)] for j in range(m + 1)]
for i in range(m + 1):
for j in range(n + 1):
if i == 0 or j == 0:
table[i][j] = 0
else:
if s[i - 1] == t[j - 1]:
table[i][j] = 1 + table[i - 1][j - 1]
else:
table[i][j] = max(table[i][j - 1], table[i - 1][j])
return m + n - table[m][n]
# Test the solution
ob = Solution()
s = "pipe"
t = "people"
result = ob.solve(s, t)
print(f"Shortest supersequence length: {result}")
Shortest supersequence length: 7
How It Works
For strings "pipe" and "people":
LCS is "piple" with length 3
Total characters: 4 + 6 = 10
Shortest supersequence: 10 - 3 = 7
One possible supersequence: "pieople"
Alternative Example
def shortest_supersequence_length(s, t):
"""Find length of shortest supersequence using LCS approach"""
m, n = len(s), len(t)
# Create DP table for LCS
dp = [[0] * (n + 1) for _ in range(m + 1)]
# Fill the DP table
for i in range(1, m + 1):
for j in range(1, n + 1):
if s[i-1] == t[j-1]:
dp[i][j] = dp[i-1][j-1] + 1
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
# Length of shortest supersequence
return m + n - dp[m][n]
# Test with multiple examples
test_cases = [("pipe", "people"), ("abc", "def"), ("AGGTAB", "GXTXAYB")]
for s, t in test_cases:
length = shortest_supersequence_length(s, t)
print(f"Strings: '{s}' and '{t}' ? Shortest supersequence length: {length}")
Strings: 'pipe' and 'people' ? Shortest supersequence length: 7 Strings: 'abc' and 'def' ? Shortest supersequence length: 6 Strings: 'AGGTAB' and 'GXTXAYB' ? Shortest supersequence length: 9
Time and Space Complexity
Time Complexity: O(m × n) where m and n are string lengths
Space Complexity: O(m × n) for the DP table
Conclusion
The shortest supersequence problem is solved by finding the LCS first, then subtracting its length from the total character count. This dynamic programming approach efficiently handles the problem in O(m×n) time complexity.
---