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

前端 未结 10 1978
醉酒成梦
醉酒成梦 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 14:54

    Inspired by @cnicutars answer:

    • function Possible(array A, string s)
      • If s is empty, return true.
      • compute the array P of all strings in A that are a prefix of s.
      • If P is empty, return false.
      • for each string p in P:
        • if Possible(A with p removed, s with prefix p removed) return true
      • return false

提交回复
热议问题