字节测开三面算法题(求解)

给出一个字符串 s 和一个词表 wordDict,按照词表对字符串进行切分,排除干扰元素,返回一个子串数组,要求切割后的子串尽可能地覆盖原字符串。

示例 1:
输入:
s = "hecatsanddogllo"
wordDict = ["cat", "cats", "and", "sand", "dog"]
输出:
["cats and dog"]
["cat sand dog"]
任意一个都可。

示例 2:
输入:
s = "hcatsandog"
wordDict = ["cats", "sandog"]
输出:
["sandog"]

示例 3:
输入:
s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
输出:
["cats", "and"]
["cats", "dog"]
["cat", "sand"]
任意一个都可。
public List<String> wordBreak(String s, Set<String> wordDict) {
    
}

感谢室友提供的答案:   (动态规划还是难啊)
/**
 * dp[i]:表示s中前i个字符最大可以切割的子字符串数组的长度和
 * result[i]存储的 List<String>为: s中前i个字符最大可以切割的子字符串数组
 * 举个例子:s = "catsandog",    wordDict = ["cats", "dog", "sand", "and", "cat"]
 * 因为 ca 没有出现在 wordDict 中,因此 对于s中的前 0/1/2 个字符来说,
 * dp[0]==dp[1]==dp[2]==0     result[0]==result[1]==result[2]==new ArrayList<>()
 * 当i为3时,cat符合条件,因此 dp[3] = 3     result[3] = ["cat"]
 * 当i为4时,cats符合条件,因此 dp[4] = 4     result[4] = ["cats"]
 * 当i为5时,以s中第5个字符'a'为结尾的子字符串没有存在词表里,因此 dp[5] = dp[4]  result[5] = result[4] = ["cats"]
 * 同理   dp[6] = dp[5]     result[6] = result[5] = ["cats"]
 * 当i为7时,s 中 第 4个字符到第7个字符 组成的 子字符串  sand 存在于 词典里,
 * 因此 dp[7] = Math.max(dp[6], dp[4-1] + 7 - 4 + 1)  而 result[7] 也会根据dp[7]的选择,
 * 如果 dp[7] == dp[6] 那么 result[7] = result[6] = ["cats"]
 * 否则的话,result[7] = result[4-1] + s中第4个字符到第7个字符组成的子字符串 = ["cat", "sand"]
 * 后面的依次类推,最后返回 result[i] 中最后一个 List<String> 即为所求。
 */

public static List<String> wordBreak(String s, Set<String> wordDict) {
    int length = s.length();
    int[] dp = new int[length + 1];
    List<String>[] result = new ArrayList[length + 1];
    result[0] = new ArrayList<>();
    dp[0] = 0;
    for (int right = 1; right <= length; ++right) {
        int index = -1;
        int maxLen = 0;
        for (int left = 1; left <= right; ++left) {
            if (wordDict.contains(s.substring(left - 1, right))) {
                int currentLen = right - left + 1 + dp[left - 1];
                if (maxLen < currentLen) {
                    maxLen = currentLen;
                    index = left;
                }
            }
        }
        List<String> currentList;
        if (index == -1 || maxLen < dp[right - 1]) {
            dp[right] = dp[right - 1];
            currentList = new ArrayList<>(result[right - 1]);
        } else {
            dp[right] = maxLen;
            currentList = new ArrayList<>(result[index - 1]);
            currentList.add(s.substring(index - 1, right));
        }
        result[right] = currentList;
    }
    return result[length];
}







#字节跳动内推提前批##面经##校招##字节跳动##测试开发工程师#
全部评论
LeetCode140
点赞 回复 分享
发布于 2021-08-03 16:13
楼主哪个部门啊
点赞 回复 分享
发布于 2021-08-04 15:12
测开考这么难的题么
点赞 回复 分享
发布于 2021-08-04 21:00
这个题解没看懂啊
点赞 回复 分享
发布于 2021-08-10 09:30

相关推荐

目前感觉简历还有很多问题,希望各位能不吝赐教以及非常感谢这位老哥——@黑皮白袜臭脚体育生&nbsp;的项目,学完一遍感觉受益颇丰
小菜鸡只想转正:校友,我的建议是冗余的最好去掉,突出重点,比如985,211双一流的提示,专业技能调整到个人项目之后的位置。专业技能感觉写的太细了?占用篇幅最好腾出一点给项目经历,如果没写手机号和邮箱,记得加上。
点赞 评论 收藏
分享
2024-12-25 09:09
四川师范大学 运营
想和你交朋友的潜伏者要冲国企:先去沃尔玛亲身感受标准化流程体系,一两年后再跳槽国内任何零售行业,可以有更大选择权吧?
点赞 评论 收藏
分享
评论
2
8
分享

创作者周榜

更多
牛客网
牛客企业服务