Reputation: 7123
Given a string s, what is the most efficient way of identifying the shortest supersequence of s from a bag of strings? Also, the last character of s should match the last character of the superstring.
Upvotes: 5
Views: 326
Reputation: 31604
Run through every string in the bag, checking if s
is a substring using a fast string search like KMP. Check which of the superstrings is shortest. This is O(Σlength of strings in bag)
.
If you need to do the search a multiple of times, you can construct a suffix trie for each string in the bag, and merge these. Then you can do lookups in O(|s|)
.
Upvotes: 0
Reputation: 36767
Unless i misunderstood it, this problem is most certainly in P.
A naive approach would be:
Where |x| means size of x.
You can combine those steps, but it's O(|B| * max(|z|)) anyway.
Upvotes: 2