-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain3.java
More file actions
22 lines (20 loc) · 710 Bytes
/
Main3.java
File metadata and controls
22 lines (20 loc) · 710 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package HOT100;
import java.util.HashMap;
import java.util.Map;
public class Main3 {
public int lengthOfLongestSubstring(String s) {
if(s.length() == 0) {
return 0;
}
int leftPoint = 0, rightPoint = 0, maxLength = 0;
Map<Character, Integer> map = new HashMap<>();
for (rightPoint = 0; rightPoint < s.length(); rightPoint++) {
if(map.containsKey(s.charAt(rightPoint))) {
leftPoint = Math.max(leftPoint, map.get(s.charAt(rightPoint)) + 1);
}
map.put(s.charAt(rightPoint), rightPoint);
maxLength = Math.max(maxLength, rightPoint - leftPoint + 1);
}
return maxLength;
}
}