-
Notifications
You must be signed in to change notification settings - Fork 0
/
L3.java
33 lines (32 loc) · 1.12 KB
/
L3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.HashMap;
import java.util.Map;
public class L3 {
/**
* 3. Longest Substring Without Repeating Characters https://leetcode.com/problems/longest-substring-without-repeating-characters/
*
* @timeComplexity O(n)
* @spaceComplexity O(n)
*/
static class Solution {
public int lengthOfLongestSubstring(String s) {
int maxSeen = 0;
int curStart = 0;
int curEnd = 0;
Map<Character, Integer> map = new HashMap<>();
while (curEnd < s.length()) {
if (map.get(s.charAt(curEnd)) != null) {
int newStart = map.get(s.charAt(curEnd)) + 1;
// Remove all keys from curStart until newStart
for (int j = curStart; j < newStart; j++) {
map.remove(s.charAt(j));
}
curStart = newStart;
}
map.put(s.charAt(curEnd), curEnd);
maxSeen = Math.max(maxSeen, curEnd - curStart + 1);
curEnd++;
}
return maxSeen;
}
}
}