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",轮到”“,最后停止。

代码:


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容