Skip to content

Commit 82d7fd2

Browse files
committed
2 pointers
1 parent a1ef6b6 commit 82d7fd2

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public int lengthOfLongestSubstring(String s) {
3+
if(s==null || s.length()==0){
4+
return 0;
5+
}
6+
if(s.length()==1){
7+
return 1;
8+
}
9+
10+
int left=0;
11+
int right=0;
12+
int ans=0;
13+
14+
HashSet<Character> set=new HashSet<>();
15+
16+
while(right<s.length()){
17+
//to find the value of the particular index
18+
char c = s.charAt(right);
19+
20+
//if the value exists in hashset or not
21+
while(set.contains(c)){
22+
set.remove(s.charAt(left));
23+
left++;
24+
}
25+
set.add(c);
26+
ans=Math.max(ans,right-left+1);
27+
right++;
28+
29+
30+
}
31+
32+
return ans;
33+
34+
}
35+
}

0 commit comments

Comments
 (0)