当前位置:网站首页>Leetcode hot topic Hot 100 day 32: "minimum coverage substring"
Leetcode hot topic Hot 100 day 32: "minimum coverage substring"
2022-07-06 20:32:00 【Ultimate brocade】
Continue to brush LeetCode Hot topic HOT 100 The subject of , And update my blog solutions. stay csdn In my blog, I will try to explain clearly in words , relevant Java Code, you can go to my Personal blog jinhuaiyu.com View in .
subject : Minimum cover substring
Give you a string s 、 A string t . return s Covered by t The smallest string of all characters . If s There is no coverage in t Substring of all characters , Returns an empty string “” .
Be careful :
about t Duplicate characters in , The number of characters in the substring we are looking for must not be less than t The number of characters in the .
If s There is such a substring in , We guarantee that it is the only answer .
Example 1:
Input :s = “ADOBECODEBANC”, t = “ABC”
Output :“BANC”
Example 2:
Input :s = “a”, t = “a”
Output :“a”
Example 3:
Input : s = “a”, t = “aa”
Output : “”
explain : t Two characters in ‘a’ Shall be included in s In the string of ,
Therefore, there is no qualified substring , Returns an empty string .
Tips :
1 <= s.length, t.length <= 105
s and t It's made up of English letters
Advanced : You can design one in o(n) The algorithm to solve this problem in time ?
solution: The sliding window
If we use violence to solve this problem , Each time we discuss the qualified substring with a position character as the initial character , You need to O(n²) Time complexity of . The optimization point is if you find a cover string , Then in the inner loop, the characters on the right side don't need to be traversed ( The initial character is fixed ), Because the longer length is not what we require . But the time complexity has not changed . Can the minimum covering substring of this fixed initial character continue to the next in the outer loop ( The initial character is shifted one bit to the right ) Continue to use ? In this case, the inner loop is no longer traversed from the initial character . This is the idea of sliding window in this problem .
Imagine in a string s There is a sliding window on the , At first, the left pointer is at the first character , The right pointer starts traversing from left to right , Until you just get to a certain position , The overwritten characters contain strings t All the characters in . At this point, we get a possible result , We recorded . Next, the right pointer does not move , The left pointer starts to move right , Until it just destroys the previous conditions ( The window no longer contains s All characters ), In the process of moving the left pointer to the right ( Not including when the best conditions are not tenable ), You will get some smaller coverage substrings , It can be used to update the results .
The left pointer no longer moves , Start moving the right pointer to the right again , Until it meets the conditions when it reaches a certain position , Then move the left pointer , This process is the same as before . We can find out , First move the right pointer to find the tail of the covering substring , When the left pointer moves, all qualified substrings will be found , In this process, we always maintain the smallest covering substring . Finally, the left and right pointers traverse the string s, Find the minimum covering substring , But there are two cycles in this process ( The outer loop moves the right pointer to find the qualified substring , Move the left pointer in an inner loop to minimize the coverage ), But the left and right pointers just moved from left to right n position , The time complexity of this process is O(n).
How to be in O(n) Determine whether the window contains t String ? We can use two hash The table stores the window and the string respectively t The number of corresponding characters in ( Window hash There is no need to store tables t Characters not in , Reduce storage space ), When comparing, you can use iterators in O(n) Compare the two within the time complexity hash surface .
Finally, The code with detailed comments is placed in my Personal blog http://jinhuaiyu.com/leetcode-minimum-window-substring/
边栏推荐
- 电子游戏的核心原理
- 看过很多教程,却依然写不好一个程序,怎么破?
- New generation garbage collector ZGC
- Le lancement du jupyter ne répond pas après l'installation d'Anaconda
- 【微信小程序】运行机制和更新机制
- Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
- 02 basic introduction - data package expansion
- B-杰哥的树(状压树形dp)
- 【Yann LeCun点赞B站UP主使用Minecraft制作的红石神经网络】
- [DIY]如何制作一款个性的收音机
猜你喜欢

01 基础入门-概念名词

电子游戏的核心原理

Tencent byte Alibaba Xiaomi jd.com offer got a soft hand, and the teacher said it was great

【DSP】【第二篇】了解C6678和创建工程
![[weekly pit] information encryption + [answer] positive integer factorization prime factor](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[weekly pit] information encryption + [answer] positive integer factorization prime factor

Rhcsa Road

2022 refrigeration and air conditioning equipment installation and repair examination contents and new version of refrigeration and air conditioning equipment installation and repair examination quest
Tencent T4 architect, Android interview Foundation

“罚点球”小游戏

【每周一坑】计算100以内质数之和 +【解答】输出三角形
随机推荐
JS get browser system language
BeagleBoneBlack 上手记
Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
I've seen many tutorials, but I still can't write a program well. How can I break it?
[weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100
8086指令码汇总表(表格)
2022 Guangdong Provincial Safety Officer C certificate third batch (full-time safety production management personnel) simulation examination and Guangdong Provincial Safety Officer C certificate third
2022 refrigeration and air conditioning equipment installation and repair examination contents and new version of refrigeration and air conditioning equipment installation and repair examination quest
棋盘左上角到右下角方案数(2)
2022 portal crane driver registration examination and portal crane driver examination materials
【计网】第三章 数据链路层(4)局域网、以太网、无线局域网、VLAN
【DSP】【第二篇】了解C6678和创建工程
Le lancement du jupyter ne répond pas après l'installation d'Anaconda
2022 nurse (primary) examination questions and new nurse (primary) examination questions
String length limit?
【每周一坑】计算100以内质数之和 +【解答】输出三角形
Unity makes AB package
Unity writes a timer tool to start timing from the whole point. The format is: 00:00:00
(work record) March 11, 2020 to March 15, 2021
rt-thread i2c 使用教程