File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .*;
2+ class Solution {
3+ public int lengthOfLongestSubstring (String s ) {
4+
5+ if (s .length () == 0 ) return 0 ;
6+ if (s .length () == 1 ) return 1 ;
7+
8+ // sliding window
9+ HashSet <Character > table = new HashSet <>();
10+
11+ int left = 0 ;
12+ int right = 1 ;
13+ int end = s .length () - 1 ;
14+
15+ table .add (s .charAt (left ));
16+
17+ int maxSize = 0 ;
18+ while (right <= end ) {
19+
20+ if (!table .contains (s .charAt (right ))) {
21+ table .add (s .charAt (right ));
22+ } else {
23+ /** [중복된 문자면 슬라이딩 윈도우 이동]
24+ 1. 중복 문자를 만나면 left를 한칸씩 증가
25+ 2. 중복 문자가 Hash 에서 사라질 때까지 왼쪽 값 제거
26+ 3. 왼쪽 중복문자가 제거되었을 때 right 이동
27+ */
28+ while (table .contains (s .charAt (right ))) {
29+ table .remove (s .charAt (left ));
30+ left ++;
31+ }
32+ table .add (s .charAt (right ));
33+ }
34+
35+ int tableSize = table .size ();
36+ if (tableSize > maxSize ) maxSize = tableSize ;
37+
38+ right ++;
39+ }
40+
41+ return maxSize ;
42+ }
43+
44+ }
45+
You can’t perform that action at this time.
0 commit comments