美文网首页
624. Remove Substrings

624. Remove Substrings

作者: 鸭蛋蛋_8441 | 来源:发表于2019-08-14 08:03 被阅读0次

    Description

    Given a string s and a set of n substrings. You are supposed to remove every instance of those n substrings from s so that s is of the minimum length and output this minimum length.

    Example

    Example 1:

    Input:

    "ccdaabcdbb"

    ["ab","cd"]

    Output:

    2

    Explanation:

    ccdaabcdbb -> ccdacdbb -> cabb -> cb (length = 2)

    Example 2:

    Input:

    "abcabd"

    ["ab","abcd"]

    Output:

    0

    Explanation:

    abcabd -> abcd -> "" (length = 0)

    思路:

    用BFS遍历每一个去掉某个字符串之后的字符串,如"abcabd",加入队列,去掉"ab"分别得到"cabd","abcd"加入队列,然后去掉"abcd",然后轮到"cabd'去掉ab得到’cd'加入队列, 去掉abcd,轮到"abcd", 去掉"ab",去掉"abcd"得到“”, 轮到"cd",轮到”“,最后停止。

    代码:

    相关文章

      网友评论

          本文标题:624. Remove Substrings

          本文链接:https://www.haomeiwen.com/subject/ajdwjctx.html