-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.java
More file actions
30 lines (25 loc) · 775 Bytes
/
3.java
File metadata and controls
30 lines (25 loc) · 775 Bytes
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
// https://leetcode.com/problems/longest-substring-without-repeating-characters/
public class Solution {
public int lengthOfLongestSubstring(String s) {
int max = 0;
int cur = 0;
HashMap<Character, Integer> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (map.containsKey(c)) {
if ((i - cur) > map.get(c)) {
map.put(c, i);
cur++;
} else {
cur = i - map.get(c);
map.put(c, i);
}
} else {
map.put(c, i);
cur++;
}
max = Math.max(cur, max);
}
return max;
}
}