Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save deeheem/776ab95a6f7aca5a641dac019f555f0a to your computer and use it in GitHub Desktop.
Save deeheem/776ab95a6f7aca5a641dac019f555f0a to your computer and use it in GitHub Desktop.
public int lengthOfLongestSubString(String s) {
int n = s.length();
Set<Character> set = new HashSet<>();
int ans = 0, i = 0, j = 0;
while (i < n && j < n) {
if (!set.contains(s.charAt(j))) {
set.add(s.charAt(j));
j++;
ans = Math.max(ans, j - i);
} else {
set.remove(s.charAt(i));
i++;
}
}
return ans;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment