(细节题)[LeetCode]Minimum Window Substring
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
Question
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
For example,
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is "BANC"
.
Note:
If there is no such window in S that covers all characters in T, return the empty string ""
.
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
本题难度Hard。
双指针法
【复杂度】
时间 O(N) 空间 O(1)
【思路】
用一个数组记录目标字符串每个字母的个数,一个数组记录窗口中每个字母的个数(字符的ascii码就是数组下标)。当找到有效的窗口后(满足条件found==t.length()
),再将左边界尽量的右缩,右缩的条件是窗口中字母的个数不小于目标字符串中字母的个数,目的是清除无效字符,比如:
然后与minLen
判断是否为更小值,如果是则刷新最小窗口的minLen
以及其边界begin
和end
。然后还要让start
向右移动一次以便找下一个有效窗口,这步不是多余的,对于:
【附】
[Leetcode] Minimum Window Substring 最小字符串窗口中对于本文代码第19行的条件是:start < i && destHash[S.charAt(start)] > srcHash[S.charAt(start)]
,实际上并不需要start < i
,因为start
不可能能大于i
。
【代码】
参考
[Leetcode] Minimum Window Substring 最小字符串窗口
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |