Algorithm for checking if a string was built from a list of substrings

前端 未结 10 1994
醉酒成梦
醉酒成梦 2021-02-02 14:35

You are given a string and an array of strings. How to quickly check, if this string can be built by concatenating some of the strings in the array?

This is a theoretica

10条回答
  •  悲哀的现实
    2021-02-02 15:06

    Let me suggest using Suffix Trees (using Ukkonen's online algorithm to build it) which seems to be suitable in terms of searching common substrings in two texts. You could find more information in wikipedia/special sources. The task is

    Find all z occurrences of the patterns P1..Pn of total length m
    enter code hereas substrings in O(m + z) time.
    

    so you see there exists very cool solution. Hope this will work for you. This is actually more suitable for repeating scans, rather than a single scan.

提交回复
热议问题