Skip to content

Commit 1ff586c

Browse files
committed
longest-substring-without-repeating-characters
1 parent 7b59476 commit 1ff586c

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

  • longest-substring-without-repeating-characters
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
투포인터를 이용한 방식
3+
문자열 s의 길이 -> N
4+
시간 복잡도 : O(N)
5+
공간 복잡도 : O(N)
6+
*/
7+
8+
class Solution {
9+
public int lengthOfLongestSubstring(String s) {
10+
if(s.length()==0){
11+
return 0;
12+
}
13+
Set<Character> set =new HashSet<>();
14+
int start=0;
15+
int end=0;
16+
int maxSize=0;
17+
int len = s.length();
18+
while(start < len){
19+
20+
21+
for(int i=end; i < len;i++){
22+
char ch = s.charAt(i);
23+
if(set.contains(ch)){
24+
end = i;
25+
break;
26+
}
27+
set.add(ch);
28+
}
29+
30+
maxSize = Math.max(maxSize, set.size());
31+
set.remove(s.charAt(start));
32+
start++;
33+
34+
}
35+
return maxSize;
36+
}
37+
}

0 commit comments

Comments
 (0)