当前位置:网站首页>Leetcode-76- minimum covering substring
Leetcode-76- minimum covering substring
2022-06-11 21:27:00 【z754916067】
subject

Ideas
- shock , It was sliding window again
- Will the required letters exist hashMap Inside , Maintain with sliding window , See the code , Too lazy to write. .
Code
class Solution {
public String minWindow(String s, String t) {
// take t Chinese character addition hash surface
LinkedHashMap<Character,Integer> m1 = new LinkedHashMap();
for(int i=0;i<t.length();i++){
// Judge first whether there is value
if(m1.containsKey(t.charAt(i))){
m1.put(t.charAt(i),1+m1.get(t.charAt(i)));
}else m1.put(t.charAt(i),1);
}
// How many characters are needed in total
int needCount = t.length();
// Start in s Slide up the window use LR maintain At first L=0 R=0
int L=0,R=0;
int ansL=0,ansR=0;
while (L<=s.length()){
while(needCount!=0){
//R Slide right
if(R>=s.length()) break;
// Record the current R The letter of
char c = s.charAt(R);
if(m1.containsKey(c)){
// Get current value value If less than or equal to 0 Explanation is not necessary If it is greater than 0 explain needcount Sure --
int value = m1.get(c);
if(value>0) needCount--;
m1.put(c,value-1);
}else m1.put(c,-1); // Otherwise it is absolutely not important
R++;
}
// If you go here, it means that the current string already contains all You can begin to L Slide right to eliminate unnecessary elements
while(true){
// The premise to jump out is to encounter letters that cannot be deleted I met +1 Will be greater than 0 The elements of Just jump out
// If you come here needCount Greater than 0 explain R Has come to the end You can no longer perform the following operations
if(needCount>0) break;
char c = s.charAt(L);
//m1 This must include c
int value = m1.get(c);
if(value+1>0) break;
else {
L++;
m1.put(c,value+1);
}
}
if(needCount>0) break;
// It means that the sliding window has been found Record length
if((ansL==0 && ansR==0 )||(ansR-ansL>R-L)){
ansL = L;
ansR = R;
}
// Ready for the next cycle Give Way L Move right
char c = s.charAt(L);
int value = m1.get(c);
L++;
m1.put(c,value+1);
needCount++;
}
return s.substring(ansL,ansR);
}
}
边栏推荐
- 关于斜率优化
- [game theory complete information static game] strategic game
- Codeforces Round #742 (Div. 2) F. One-Four Overload
- Application business layer modification
- I haven't blogged for two months. I sent an article to prove that my account is still alive
- Codeforces Round #742 (Div. 2) F. One-Four Overload
- [Part 13] source code analysis and application details of completabilefuture class [key]
- Go语言函数
- 八、BOM - 章节课后练习题及答案
- Stream Chinese sorting
猜你喜欢

One article to show you how to understand the harmonyos application on the shelves

JVM object allocation policy TLAB
![[Part 15] use and basic principle of forkjoinpool [key]](/img/36/e21b16ec92d444149bc793f340f9f3.jpg)
[Part 15] use and basic principle of forkjoinpool [key]

2021牛客多校5 Double Strings

Test plans and test cases

Flutter implements the JD address selection component

Goto statement of go language

A collection of commonly used open source data sets for face recognition

flutter系列之:flutter中常用的container layout详解

Codeforces Round #744 (Div. 3) 解题报告
随机推荐
Answer fans' questions | count the number and frequency of letters in the text
Deriving Kalman filter from probability theory
2022年6月9日 16:29:41 日记
How to manually drag nodes in the Obsidian relationship graph
[Part 15] use and basic principle of forkjoinpool [key]
杭电中超9 1006 Guess the Weight
ORA-04098: trigger ‘xxx. xxx‘ is invalid and failed re-validation
Codeforces Round #740 Div. 2 解题报告
[Part 14] source code analysis and application details of completionservice class [key]
作为一名 ABAP 资深顾问,下一步可以选择哪一门 SAP 技术作为主攻方向?
Test plans and test cases
How to import workflows provided on SAP API hub to sap BTP
JVM之对象创建过程
使用 SAP UI5 CLI 命令行工具构建和运行 SAP UI5 应用
Go language conditional statement
bzoj3188 Upit
JVM运行时常量池以及直接内存
[Part 16] copyonwritearraylist source code analysis and application details [key]
Field queryIndexFieldnameService in xxxImpl required a single bean, but 19 were found:
Go语言条件语句