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

前端 未结 10 1971
醉酒成梦
醉酒成梦 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

    This is how I would do it.

    1. Determine the length of the target string.
    2. Determine the length of each string in the substring array
    3. Determine which combination of substrings would yield a string with the same length as the target string (If any, if not you're done)
    4. Generate all permutations of the substring combinations determined in step 3. Check if any of them match the target string.

    Generating all permutations is a processor heavy task, so if you can cut down on your 'n' (input size), you'll gain some considerable efficiency.

提交回复
热议问题