【算法题】2707. 字符串中的额外字符

2023-05-31  本文已影响0人  程序员小2

题目:

给你一个下标从 0 开始的字符串 s 和一个单词字典 dictionary 。你需要将 s 分割成若干个 互不重叠 的子字符串,每个子字符串都在 dictionary 中出现过。s 中可能会有一些 额外的字符 不在任何子字符串中。

请你采取最优策略分割 s ,使剩下的字符 最少 。

示例 1:

输入:s = "leetscode", dictionary = ["leet","code","leetcode"]
输出:1
解释:将 s 分成两个子字符串:下标从 0 到 3 的 "leet" 和下标从 5 到 8 的 "code" 。只有 1 个字符没有使用(下标为 4),所以我们返回 1 。
示例 2:

输入:s = "sayhelloworld", dictionary = ["hello","world"]
输出:3
解释:将 s 分成两个子字符串:下标从 3 到 7 的 "hello" 和下标从 8 到 12 的 "world" 。下标为 0 ,1 和 2 的字符没有使用,所以我们返回 3 。

提示:

1 <= s.length <= 50
1 <= dictionary.length <= 50
1 <= dictionary[i].length <= 50
dictionary[i] 和 s 只包含小写英文字母。
dictionary 中的单词互不相同。

java代码:

class Solution {
    public int minExtraChar(String s, String[] dictionary) {

        set = new HashSet<>();
        maxLen = 0;
        for (String str : dictionary) {
            if (str.length() > maxLen) {
                maxLen = str.length();
            }
            set.add(str);
        }
        dp = new int[s.length()];
        Arrays.fill(dp, -1);
        int ans = dfs(0, s);
        return ans;
    }

    int maxLen;
    Set<String> set;
    int[] dp;

    private int dfs(int index, String s) {
        if (index == s.length()) {
            return 0;
        }
        if (dp[index] != -1) {
            return dp[index];
        }
        int curr = s.length();
        for (int len = 1; len <= maxLen && index + len <= s.length(); len++) {
            String currStr = s.substring(index, index + len);
            if (set.contains(currStr)) {
                curr = Math.min(curr, dfs(index + len, s));
            } else {
                curr = Math.min(curr, len + dfs(index + len, s));
            }
        }
        dp[index] = curr;
        return curr;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读