Problem Link
https://leetcode.com/problems/longest-substring-without-repeating-characters/
Detailed Step-by-Step Explanation
Solution
class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> map = new HashMap<>();
int left = 0;
int maxLength = 0;
for (int right = 0; right < s.length(); right++) {
char ch = s.charAt(right);
if (map.containsKey(ch)) {
left = Math.max(left, map.get(ch) + 1);
}
map.put(ch, right);
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
}
}

Top comments (0)