leetcode 每日一题 2024年01月09日 字符串中的额外字符

题目

字符串中的额外字符

给你一个下标从 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 中的单词互不相同。

提示 1

Can we use Dynamic Programming here?

提示2

Define DP[i] as the min extra character if breaking up s[0:i] optimally.

分析

  1. 首先要注意的是dictionary的职责是字典,每个字符串是可以重复利用的。

  2. 根据提示我们可以用动态规划,来计算,用dp[i]表示前i-1个字符剩余的最少额外字符。

  3. 输入:s = “leetscode”, dictionary = [“leet”,“code”,“leetcode”,“et”]

    那么我们观察dp[3]=3
    当i=4 时,对应字符leet,dp[4]=dp[3]+1=4
    et是可以匹配上的dp[4]可以所见到dp[2]
    leet是被匹配上的,所以dp[4]可以缩减到dp[0]
    

    当匹配到第i个字符时,我们需要从 i往前找到0位置(边界为j),找到可以发生状态转换的地方,并像给出的例子中一样找到这其中其中最小的dp[j]。
    dp[i] = min(dp[i],Set(dp[j]))

  4. 所以有:
    对于一般情况:dp[i] = dp[i-1]+1
    对于每个dp[i] 往前找: dp[i] = min(dp[i],Set(dp[j])),j∈[0,i-1]

编码

class Solution {
   
    public int minExtraChar(String s, String[] dictionary) {
   
        int [] dp = new int[s.length()+1];
        dp[0] = 0;
        HashSet<String> dicSet = new HashSet<>();
        Collections.addAll(dicSet, dictionary);
        for (int i = 1; i <= s.length(); i++) {
   
            dp[i] = dp[i-1]+1;
            for (int j = i-1; j >= 0; j--) {
   
                //这是s全部的字串
              if(dicSet.contains(s.substring(j,i))){
   
                  dp[i] = Math.min(dp[i],dp[j]);
              }
            }
        }
        return dp[dp.length-1];
    }
}

交流

在这里插入图片描述

相关推荐

  1. LeetCode每日 | 2707. 字符串额外字符

    2024-01-09 18:46:03       42 阅读
  2. 2024.1.9力扣每日——字符串额外

    2024-01-09 18:46:03       31 阅读
  3. 【力扣每日】力扣2707字符串额外字符

    2024-01-09 18:46:03       34 阅读
  4. LeetCode 2707. 字符串额外字符

    2024-01-09 18:46:03       40 阅读
  5. LeetCode每周_2024/01/01~2024/01/05

    2024-01-09 18:46:03       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-09 18:46:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-09 18:46:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-09 18:46:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-09 18:46:03       18 阅读

热门阅读

  1. Kafka单节点部署

    2024-01-09 18:46:03       28 阅读
  2. php实现定时任务

    2024-01-09 18:46:03       31 阅读
  3. LC509. 斐波那契数

    2024-01-09 18:46:03       34 阅读
  4. Linux自动化部署脚本

    2024-01-09 18:46:03       40 阅读
  5. Object-c初步学习 四

    2024-01-09 18:46:03       31 阅读
  6. 【低功耗】芯片低功耗-硬件

    2024-01-09 18:46:03       37 阅读
  7. 彻底卸载Microsoft Edge:一步步指南

    2024-01-09 18:46:03       40 阅读
  8. 骑砍战团MOD开发(34)-光照系统

    2024-01-09 18:46:03       43 阅读