Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1ffa063

Browse files
authored
Merge pull requestneetcode-gh#10 from anthonysim/asim/minwindow
Added JS 76-Min-Window
2 parentsf71b78d +266c0a0 commit1ffa063

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
varminWindow=function(str,target){
2+
consthash=target.split('').reduce((acc,val)=>{
3+
if(!acc[val])acc[val]=0;
4+
acc[val]+=1;
5+
returnacc;
6+
},{})
7+
8+
letstart=0;
9+
letmin=Infinity;
10+
letmatched=0;
11+
letsubStringStart=null;
12+
13+
for(leti=0;i<str.length;i++){
14+
letrightChar=str[i];
15+
16+
if(rightCharinhash)hash[rightChar]-=1;
17+
if(hash[rightChar]>=0)matched+=1;
18+
19+
while(matched===target.length){
20+
if(i-start+1<min){
21+
subStringStart=start;
22+
min=i-start+1;
23+
}
24+
25+
letleftChar=str[start];
26+
start+=1;
27+
28+
if(leftCharinhash){
29+
if(hash[leftChar]===0)matched-=1;
30+
hash[leftChar]+=1;
31+
}
32+
}
33+
}
34+
returnmin===Infinity ?'' :str.substring(subStringStart,subStringStart+min);
35+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp