问题
暴力破解方法:
TC:O(N^2),SC:O(256),这是常数
注意:这将导致 TLE
class Solution { public String minWindow(String s, String t) { int min = Integer.MAX_VALUE; int start =-1; int end = -1; //generating all possible substrings and returning the smallest substring having //all the character of string t for(int i =0;i<s.length int arr new all the asci characters with count of character in t for k="0;k<t.length();k++){" j="i;j<s.length();j++){" if at s is also present increase>0){ count++; } arr[s.charAt(j)]--;//decrease the frequency of s.charAt(j) by 1 in arr if(count ==t.length()){// if count == length of t we have found one potential substring if(min> j-i+1){ min = j-i +1; start = i; end = j; } break;// break out as there is no need to propagate further in the current iteration } } } if(start ==-1 || end ==-1) return "";// if not found return s.substring(start,end+1);// else } } </s.length>
最佳方法:
TC: O(n) , SC:O(256) 是常数
// this intuition will require solving such problems as much as possible class Solution { public String minWindow(String s, String t) { int left=0,right=0; int min = Integer.MAX_VALUE; int count =0; int arr[] = new int[256]; // has table for keeping count of characters // keeping track of start and end index int start =-1; int end = -1; for(int i=0;i<t.length arr while char c="s.charAt(right);" if>0){ // if the character is part of t, then increment count count++; } arr[c]--;// decrease the count of the character in the hash table once it is taken into consideration while(count ==t.length()){ // if count == t.size() we have found a potential window if(min > right-left+1){ min = right-left+1; //update the start and end accordingly start = left; end = right; } arr[s.charAt(left)]++;//since we are removing this character from window and increasing the left pointer to point to next character after this, there will be one less //character that is not part of the t if(arr[s.charAt(left)]>0){ // if after doing +1 for arr[s.charAt(left)] character, if it become positive then it will mean that this was part of t, and now we will have to look for this character in the next window count--; // since this was part of t, then count should decrement by 1 } left++; } right++; } if(start ==-1 || end ==-1) return ""; return s.substring(start,end+1); } } </t.length>
以上是最小窗口子串的详细内容。更多信息请关注PHP中文网其他相关文章!
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
刺客信条阴影:贝壳谜语解决方案
2 周前ByDDD
R.E.P.O.如果您听不到任何人,如何修复音频
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver CS6
视觉化网页开发工具

Dreamweaver Mac版
视觉化网页开发工具