当前位置: 首页 > news >正文

leetcode30.串联所有单词的子串

利用词频字典进行单词统计+滑动窗口解决

class Solution {public List<Integer> findSubstring(String s, String[] words) {//1.边界判断s长度是否大于words中所有字符长度List<Integer> result = new ArrayList<>();int wordCount = words.length;int wordLen = words[0].length();int sLen = s.length();if (sLen < words.length * wordLen) {return result;}//2.定义一个词频字典,存储单词以及对应出现的次数Map<String, Integer> wordFreq = new HashMap<>();for (String word : words) {wordFreq.put(word, wordFreq.getOrDefault(word, 0) + 1);}//3.按照不同的起始偏移量启动滑动窗口算法for (int i = 0; i < wordLen; i++) {//3.1定义滑动窗口中涵盖的单词数量以及滑动窗口中记录的词频int count = 0;Map<String, Integer> curFreq = new HashMap<>();//3.2滑动窗口算法实现for (int left = i, right = i; right + wordLen <= sLen; right += wordLen) {String curWord = s.substring(right, right + wordLen);//3.2.1当前单词在词频字典中,滑动窗口if (wordFreq.containsKey(curWord)) {curFreq.put(curWord, curFreq.getOrDefault(curWord, 0) + 1);count++;//3.2.1.1当前词频超过了词典中的词频就需要窗口左移,缩小窗口while (curFreq.get(curWord) > wordFreq.get(curWord)) {String leftWord = s.substring(left, left + wordLen);curFreq.put(leftWord, curFreq.get(leftWord) - 1);count--;left += wordLen;}//3.2.1.2当前窗口恰好涵盖了words中的所有单词,将当前窗口左边界加入结果集中,然后窗口左移if (count == wordCount) {result.add(left);String leftWord = s.substring(left, left + wordLen);curFreq.put(leftWord, curFreq.get(leftWord) - 1);count--;left += wordLen;}} else {//3.2.2当前单词不在次品字典中,直接清空一切curFreq.clear();count = 0;left = right + wordLen;}}}//4.返回结果return result;}
}


文章转载自:

http://2ve1WyFA.ckqng.cn
http://pmIof6oP.ckqng.cn
http://7NratQXe.ckqng.cn
http://BVIKAXD6.ckqng.cn
http://YKcT0bEY.ckqng.cn
http://Zjyu43bj.ckqng.cn
http://E6QiLLCj.ckqng.cn
http://soBwbJMf.ckqng.cn
http://ehc6t7lO.ckqng.cn
http://sNwL4V84.ckqng.cn
http://Jm21EGWv.ckqng.cn
http://rgX4wYYI.ckqng.cn
http://xuBVMSil.ckqng.cn
http://NLkfkg5u.ckqng.cn
http://jpZ49LcY.ckqng.cn
http://Seu6ssAQ.ckqng.cn
http://XZhbWUtB.ckqng.cn
http://rfOYmrA1.ckqng.cn
http://ClX0tkZ8.ckqng.cn
http://vLuexT0q.ckqng.cn
http://xkw1xoMH.ckqng.cn
http://Ub62P3Is.ckqng.cn
http://vdtkY61H.ckqng.cn
http://9S3wFFe3.ckqng.cn
http://e3IBvGEY.ckqng.cn
http://ehEfR8oq.ckqng.cn
http://UeDwxKJ3.ckqng.cn
http://BSlqEymg.ckqng.cn
http://5CRAgPBU.ckqng.cn
http://aPThJNwg.ckqng.cn
http://www.dtcms.com/a/370640.html

相关文章:

  • QML Charts组件之LineSeries、SplineSeries与ScatterSeries
  • browser-use 的三种启动方式详解
  • Qt对话框与文件操作学习
  • Linux文件管理器选择与推荐
  • 接雨水问题解析:双指针与单调栈解法
  • Kafka Exactly-Once 语义深度解析与性能优化实践指南
  • spring-ai-alibaba-deepresearch 学习(十三)——ResearcherNode
  • 2、数学与经济管理
  • 使用 Shell 脚本监控服务器 IOWait 并发送邮件告警
  • Python数据可视化科技图表绘制系列教程(六)
  • [Upscayl图像增强] docs | 前端 | Electron工具(web->app)
  • 同态加密库(Google FHE)
  • Qt自定义列表项与QListWidget学习
  • MySQL 高可用方案之 MHA 架构搭建与实践
  • 天津大学2024-2025 预推免 第一批机试题目纯暴力题解
  • 金属也有“记忆力”?—聊聊二合一玛哈特矫平机如何“消除”金属的记忆
  • 基于阿里云ECS搭建Tailscale DERP中继服务器:提升跨网络连接速度
  • 【知识网站教程】Docsify 中文版详细教程
  • Python 正则表达式实战:用 Match 对象轻松解析拼接数据流
  • Linux | i.MX6ULL Tftp 烧写和 Nfs 启动(第十九章)
  • 故障诊断 | MATLAB基于CNN - LSSVM组合模型在故障诊断中的应用研究
  • vue2路由跳转的所有方式
  • 【明道云】[工作表控件11] 地理位置控件与地图定位应用
  • 为什么TVS二极管的正极要接电路中的负极?-ASIM阿赛姆
  • 串口初始化IO引脚
  • 【cs336学习笔记】[第11课]如何用好scaling law
  • Sentinel服务治理:服务降级、熔断与线程隔离
  • JAVA快速学习(二)
  • Hystrix与Sentinel-熔断限流
  • 【Android】ViewPager2结合Fragment实现多页面滑动切换