> 文章列表 > 【LeetCode: 面试题 17.13. 恢复空格 | 暴力递归=>记忆化搜索=>动态规划】

【LeetCode: 面试题 17.13. 恢复空格 | 暴力递归=>记忆化搜索=>动态规划】

【LeetCode: 面试题 17.13. 恢复空格 | 暴力递归=>记忆化搜索=>动态规划】

在这里插入图片描述

🚀 算法题 🚀

🌲 算法刷题专栏 | 面试必备算法 | 面试高频算法 🍀
🌲 越难的东西,越要努力坚持,因为它具有很高的价值,算法就是这样✨
🌲 作者简介:硕风和炜,CSDN-Java领域新星创作者🏆,保研|国家奖学金|高中学习JAVA|大学完善JAVA开发技术栈|面试刷题|面经八股文|经验分享|好用的网站工具分享💎💎💎
🌲 恭喜你发现一枚宝藏博主,赶快收入囊中吧🌻
🌲 人生如棋,我愿为卒,行动虽慢,可谁曾见我后退一步?🎯🎯

🚀 算法题 🚀

在这里插入图片描述

🍔 目录

    • 🚗 知识回顾
    • 🚩 题目链接
    • ⛲ 题目描述
    • 🌟 求解思路&实现代码&运行结果
      • ⚡ 暴力递归
        • 🥦 求解思路
        • 🥦 实现代码
        • 🥦 运行结果
      • ⚡ 记忆化搜索
        • 🥦 求解思路
        • 🥦 实现代码
        • 🥦 运行结果
      • ⚡ 动态规划
        • 🥦 求解思路
        • 🥦 实现代码
        • 🥦 运行结果
    • 💬 共勉

🚗 知识回顾

在开始下面这道面试题目的时候,我们可以先看一下我之前和该题目具有相同求解思路的一道题目,学习完该题目再看这道题目就会非常简单了。
博客地址:【LeetCode: 139. 单词拆分 | 暴力递归=>记忆化搜索=>动态规划】

🚩 题目链接

  • 面试题 17.13. 恢复空格

⛲ 题目描述

哦,不!你不小心把一个长篇文章中的空格、标点都删掉了,并且大写也弄成了小写。像句子"I reset the computer. It still didn’t boot!“已经变成了"iresetthecomputeritstilldidntboot”。在处理标点符号和大小写之前,你得先把它断成词语。当然了,你有一本厚厚的词典dictionary,不过,有些词没在词典里。假设文章用sentence表示,设计一个算法,把文章断开,要求未识别的字符最少,返回未识别的字符数。

注意:本题相对原题稍作改动,只需返回未识别的字符数

示例:

输入:
dictionary = [“looked”,“just”,“like”,“her”,“brother”]
sentence = “jesslookedjustliketimherbrother”
输出: 7
解释: 断句后为"jess looked just like tim her brother",共7个未识别字符。
提示:

0 <= len(sentence) <= 1000
dictionary中总字符数不超过 150000。
你可以认为dictionary和sentence中只包含小写字母。

🌟 求解思路&实现代码&运行结果


⚡ 暴力递归

🥦 求解思路

  1. 核心的思路和我们上面提到的题目思路是一样的,此处不做过多讲解,如果不懂的同学可以看上一篇博客题解。
  2. 首先,不一样的地方首先在于求解的结果不同,这个是显而易见的。
  3. 其次,最重要的一点是【LeetCode: 139. 单词拆分】中是看所有字典中的字符串是否可以组成给定的s,如果都不可以,直接返回false即可,但是该题目不同的是如果都不可以,我们还进行下一个位置的判断,并且执行加1的操作,从下一个位置继续重复上面的过程。这点是很重要的。

🥦 实现代码

class Solution {public int respace(String[] dictionary, String sentence) {return process(0,sentence,dictionary);}public int process(int index,String s,String[] dictionary){if(index>=s.length()) return 0;int ans=Integer.MAX_VALUE;for(int i=0;i<dictionary.length;i++){String dict=dictionary[i];if(s.startsWith(dict,index)){ans=Math.min(ans,process(index+dict.length(),s,dictionary));}}ans=Math.min(ans,process(index+1,s,dictionary)+1);return ans;}
}

🥦 运行结果

【LeetCode: 面试题 17.13. 恢复空格 | 暴力递归=>记忆化搜索=>动态规划】


⚡ 记忆化搜索

🥦 求解思路

  1. 根据我们递归的分析,在递归的过程中会产生重复的子过程,所以我们想到了加一个缓存表,也就是我们的记忆化搜索。

🥦 实现代码

class Solution {int[] dp;public int respace(String[] dictionary, String sentence) {dp=new int[sentence.length()];Arrays.fill(dp,-1);return process(0,sentence,dictionary);}public int process(int index,String s,String[] dictionary){if(index>=s.length()) return 0;if(dp[index]!=-1) return dp[index];int ans=Integer.MAX_VALUE;for(int i=0;i<dictionary.length;i++){String dict=dictionary[i];if(s.startsWith(dict,index)){ans=Math.min(ans,process(index+dict.length(),s,dictionary));}}ans=Math.min(ans,process(index+1,s,dictionary)+1);return dp[index]=ans;}
}

🥦 运行结果

【LeetCode: 面试题 17.13. 恢复空格 | 暴力递归=>记忆化搜索=>动态规划】


⚡ 动态规划

🥦 求解思路

  1. 接下来我们根据之前的递归思路以及记忆化缓存改写动态规划。

🥦 实现代码

class Solution {public int respace(String[] dictionary, String s) {int[] dp=new int[s.length()+1];for(int index=s.length()-1;index>=0;index--){int ans=Integer.MAX_VALUE;for(int i=0;i<dictionary.length;i++){String dict=dictionary[i];if(s.startsWith(dict,index)){ans=Math.min(ans,dp[index+dict.length()]);}}ans=Math.min(ans,dp[index+1]+1);dp[index]=ans;}return dp[0];}
}

🥦 运行结果

【LeetCode: 面试题 17.13. 恢复空格 | 暴力递归=>记忆化搜索=>动态规划】


💬 共勉

最后,我想送给大家一句一直激励我的座右铭,希望可以与大家共勉!
【LeetCode: 面试题 17.13. 恢复空格 | 暴力递归=>记忆化搜索=>动态规划】

在这里插入图片描述