Skip to content

Commit

Permalink
Create 3-Longest-Substring-Without-Repeating-Characters.java
Browse files Browse the repository at this point in the history
  • Loading branch information
SharmaTushar1 authored Jul 6, 2022
1 parent 41b18af commit 4469cf0
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions java/3-Longest-Substring-Without-Repeating-Characters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int lengthOfLongestSubstring(String s) {
HashSet<Character> set = new HashSet<>();
int left = 0;
int ans = 0;
for (int right = 0; right<s.length(); right++) {
while (set.contains(s.charAt(right))) {
set.remove(s.charAt(left));
left++;
}
set.add(s.charAt(right));
ans = Math.max(ans, right-left+1);
}
return ans;
}
}

0 comments on commit 4469cf0

Please sign in to comment.