Split a string to a string of valid words using Dynamic Programming

后端 未结 6 1193
不知归路
不知归路 2020-12-23 15:02

I need to find a dynamic programming algorithm to solve this problem. I tried but couldn\'t figure it out. Here is the problem:

You are given a string of n character

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-23 15:42

    To formalize what @MinhPham suggested.

    This is a dynammic programming solution.

    Given a string str, let

    b[i] = true if the substring str[0...i] (inclusive) can be split into valid words.

    Prepend some starting character to str, say !, to represent the empty word. str = "!" + str

    The base case is the empty string, so

    b[0] = true.

    For the iterative case:

    b[j] = true if b[i] == true and str[i..j] is a word for all i < j

提交回复
热议问题