Skip to content

Commit fcea209

Browse files
committed
solve : #243 (Longest Substring Without Repeating Characters) with Java
1 parent ae6f4c1 commit fcea209

File tree

1 file changed

+25
-0
lines changed
  • longest-substring-without-repeating-characters

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}

0 commit comments

Comments
ย (0)