2707. 字符串中的额外字符

牛客网:https://leetcode.cn/problems/extra-characters-in-a-string/description/?envType=daily-question&envId=2024-01-09
在这里插入图片描述

官方解题思路为动态规划或字典数优化;
这里引入Up主的解题思路(递归)

哔哩哔哩:https://www.bilibili.com/video/BV1Ee41127LX/?spm_id_from=333.337.search-card.all.click&vd_source=f385259e95f72c4536cc27a3528bd922

Up主采用python3代码过题:

class Solution:
    def minExtraChar(self, s: str, dictionary: List[str]) -> int:

        @cache
        def dfs(start):
            if start == len(s):
                return 0

            result = dfs(start + 1) + 1
            for end in range(start, len(s)):
                word = s[start: end + 1]
                if word not in dictionary:
                    continue
                result = min(result, dfs(end + 1))

            return result
        
        return dfs(0)

细看思路很简单,递归查找并比较所留最少字符的答案;
基于此思路,开启了Go语言的模仿:
第一反应,按照Up主的思路,直接dfs递归查找,结果当场TLE,debug发现,在回溯每个dfs,都会查找一次重复数据,于是开始剪枝优化

func minExtraChar(s string, dictionary []string) int {
   
	var dfs func(start int) int
	dfs = func(start int) int {
   
		if start == len(s) {
   
			return 0
		}
		result := dfs(start+1) + 1
		for end := start; end < len(s); end++ {
   
			word := s[start : end+1]
			for _, dic := range dictionary {
   
				if word != dic {
   
					continue
				}
				result = min(result, dfs(end+1))
			}
		}
		return result
	}
	return dfs(0)
}

AC代码:

func minExtraChar(s string, dictionary []string) int {
   
	cache := make(map[int]int)

	var dfs func(int) int
	dfs = func(start int) int {
   
		if start == len(s) {
   
			return 0
		}

		if val, ok := cache[start]; ok {
   
			return val
		}

		result := dfs(start+1) + 1
		for end := start; end < len(s); end++ {
   
			word := s[start : end+1]
			found := false
			for _, w := range dictionary {
   
				if word == w {
   
					found = true
					break
				}
			}
			if !found {
   
				continue
			}
			result = min(result, dfs(end+1))
		}

		cache[start] = result
		return result
	}

	return dfs(0)
}

func min(a, b int) int {
   
	if a < b {
   
		return a
	}
	return b
}

相关推荐

  1. LeetCode 2707. 字符串额外字符

    2024-01-10 00:52:06       40 阅读
  2. LeetCode每日一题 | 2707. 字符串额外字符

    2024-01-10 00:52:06       42 阅读
  3. 【力扣每日一题】力扣2707字符串额外字符

    2024-01-10 00:52:06       34 阅读
  4. 2024.1.9力扣每日一题——字符串额外

    2024-01-10 00:52:06       31 阅读
  5. Python——用新字符替换字符串字符

    2024-01-10 00:52:06       8 阅读
  6. P1098 [NOIP2007 提高组] 字符串展开

    2024-01-10 00:52:06       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-10 00:52:06       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-10 00:52:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-10 00:52:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-10 00:52:06       18 阅读

热门阅读

  1. 2024年湖北建设厅建筑七大员怎么报考?

    2024-01-10 00:52:06       37 阅读
  2. Linux 编辑器和文本处理

    2024-01-10 00:52:06       31 阅读
  3. 面试题总结(1.8)

    2024-01-10 00:52:06       27 阅读
  4. C#,C++实现:华为经典笔试题_菜单组合种类题目

    2024-01-10 00:52:06       32 阅读
  5. arch modelsim 解决无法运行

    2024-01-10 00:52:06       32 阅读
  6. 算法学习:动态规划之爬楼梯问题

    2024-01-10 00:52:06       39 阅读
  7. 学习Go语言Web框架Gee总结--中间件Middleware(五)

    2024-01-10 00:52:06       51 阅读
  8. 深入PostgreSQL:高级函数用法探索

    2024-01-10 00:52:06       43 阅读
  9. 【代码学习】einops,更简单的张量变化

    2024-01-10 00:52:06       36 阅读