FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode-study/minimum-window-substring/Tessa1217.java at main · DaleStudy/leetcode-study · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
DaleStudy
/
leetcode-study
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
361
Star
155
Code
Issues
75
Pull requests
5
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode-study
/
minimum-window-substring
/
Tessa1217.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
70 lines (52 loc) · 2.03 KB
Breadcrumbs
leetcode-study
/
minimum-window-substring
/
Tessa1217.java
Copy path
File metadata and controls
70 lines (52 loc) · 2.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import
java
.
util
.
HashMap
;
import
java
.
util
.
Map
;
class
Solution
{
// 시간복잡도: O(n), 공간복잡도 O(1)
public
String
minWindow
(
String
s
,
String
t
) {
int
strLength
=
s
.
length
();
int
targetLength
=
t
.
length
();
// 목표 분자열이 주어진 문자열보다 길다면 부분 문자열로 볼 수 없음
if
(
targetLength
>
strLength
) {
return
""
;
}
// 목표 문자열에 필요한 문자와 그 개수를 담는 맵
Map
<
Character
,
Integer
>
charMap
=
new
HashMap
<>();
for
(
char
c
:
t
.
toCharArray
()) {
charMap
.
put
(
c
,
charMap
.
getOrDefault
(
c
,
0
) +
1
);
}
// 투 포인터 선언
int
left
=
0
;
int
right
=
0
;
int
minWindowLength
=
Integer
.
MAX_VALUE
;
int
minWindowStart
=
0
;
// 최소 윈도우 시작 위치
int
remaining
=
targetLength
;
while
(
right
<
strLength
) {
Character
end
=
s
.
charAt
(
right
);
if
(
charMap
.
containsKey
(
end
)) {
charMap
.
put
(
end
,
charMap
.
get
(
end
) -
1
);
if
(
charMap
.
get
(
end
) >=
0
) {
remaining
--;
}
}
// target 문자열의 모든 문자를 찾았다면 left 포인터 이동하면서
// 최소 윈도우 찾기 시작
while
(
remaining
==
0
) {
if
(
right
-
left
+
1
<
minWindowLength
) {
minWindowLength
=
right
-
left
+
1
;
minWindowStart
=
left
;
}
char
startChar
=
s
.
charAt
(
left
);
if
(
charMap
.
containsKey
(
startChar
)) {
charMap
.
put
(
startChar
,
charMap
.
get
(
startChar
) +
1
);
if
(
charMap
.
get
(
startChar
) >
0
) {
remaining
++;
}
}
left
++;
}
right
++;
}
return
minWindowLength
==
Integer
.
MAX_VALUE
?
""
:
s
.
substring
(
minWindowStart
,
minWindowStart
+
minWindowLength
);
}
}
Back
|
FazBrowse Home
|
New Git URL