File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int lengthOfLongestSubstring (String s ) {
3
+ // HashSet ํ์ด
4
+ // ์๊ฐ๋ณต์ก๋ : O(n), ๊ณต๊ฐ๋ณต์ก๋ O(1)
5
+ // ํ์ด
6
+ // HashSet์ ๋์ผํ ๋ฌธ์๊ฐ ์๋์ง ์ฒดํฌํ๊ณ ๋์ผํ ๋ฌธ์๊ฐ ์์ผ๋ฉด ์ผ์ชฝ ๊ธฐ์ค์ ์ ํ๋์ฉ ์ด๋ (๋์ผ ๋ฌธ์๊ฐ ์์ ๋๊น์ง ๋ฐ๋ณต)
7
+ // ๋์ผํ ๋ฌธ์๊ฐ ์์ ๋ ํ์ฌ ๊ธฐ์ค(right) - ์ผ์ชฝ ๊ธฐ์ค(left) + 1 ์ ์ต๋ ๊ธธ์ด๋ฅผ maxLength ์ ์ฅ ํ ๋ฐํ
8
+
9
+ int left = 0 ;
10
+ int maxLength = 0 ;
11
+ HashSet <Character > charSet = new HashSet <>();
12
+
13
+ for (int right = 0 ; right < s .length (); right ++) {
14
+ while (charSet .contains (s .charAt (right ))) {
15
+ charSet .remove (s .charAt (left ));
16
+ left ++;
17
+ }
18
+
19
+ charSet .add (s .charAt (right ));
20
+ maxLength = Math .max (maxLength , right - left + 1 );
21
+ }
22
+
23
+ return maxLength ;
24
+ }
25
+ }
You canโt perform that action at this time.
0 commit comments