Skip to content

Commit c65779b

Browse files
committed
add longest substring without repeating characters solution
1 parent 9612086 commit c65779b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* ๋ฌธ์ž์—ด s๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ์ค‘๋ณต ๋ฌธ์ž๊ฐ€ ์—†๋Š” ๊ฐ€์žฅ ๊ธด ๋ฌธ์ž์—ด ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.
3+
* */
4+
class Solution {
5+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
6+
public int lengthOfLongestSubstring(String s) {
7+
8+
int maxLength = 0;
9+
10+
int left = 0;
11+
int right = 0;
12+
13+
// ์•ŒํŒŒ๋ฒณ (๋Œ€์†Œ๋ฌธ์ž), ์ˆซ์ž, ํŠน์ˆ˜๋ฌธ์ž, ๊ณต๋ฐฑ
14+
boolean[] visited = new boolean[128];
15+
16+
while (right < s.length()) {
17+
while (visited[s.charAt(right)]) {
18+
visited[s.charAt(left)] = false;
19+
left++;
20+
}
21+
visited[s.charAt(right)] = true;
22+
maxLength = Math.max(right - left + 1, maxLength);
23+
right++;
24+
}
25+
26+
return maxLength;
27+
}
28+
}
29+

0 commit comments

Comments
ย (0)