Shortest string containing both
InterviewTime limit1sMemory limit256 MB
Find the length of the shortest string that has both given strings as subsequences.
- Level
Medium4 of 10
- Topics
- Dynamic programming, String
- Solved
- No attempts yet
Problem
You are given two strings A and B.
A string X is a subsequence of a string S if you can delete zero or more characters from S and join the remaining characters, in their original order, to get X.
Write a program that computes the length of the shortest string S that has both A and B as subsequences.
For example, if A = "abcbdab" and B = "bdcaba", then S = "abdcabdab" has both of them as subsequences, and no S shorter than 9 works.
Input
The first line contains the string A and the second line contains the string B. Both strings consist of lowercase letters only, and each has length between 1 and 1,000.
Output
Print the length of the shortest string that has both A and B as subsequences.