624. Remove Substrings

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)