-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLongestNonRepeatSubstring.java
46 lines (35 loc) · 1.58 KB
/
LongestNonRepeatSubstring.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
34
35
36
37
38
39
40
41
42
43
44
45
46
package leetcode.string;
import java.util.HashMap;
public class LongestNonRepeatSubstring {
// Pre-condition: assume all letters in the input string is lowercase.
public int lengthOfLongestSubstring(String s) {
// A mapping from 26 letters to their index within the string. They may be removed if they
// are not within the current substring anymore.
HashMap<Character, Integer> mapping = new HashMap<>();
// The length of the longest substring.
int longest = 0;
// The starting point of the current substring.
int currentStart = 0;
for (int i = 0; i < s.length(); i++) {
char currentChar = s.charAt(i);
// Checks whether the current character exists in the mapping previously.
if (mapping.containsKey(currentChar)) {
// If so, the starting point of the current substring needs to change.
int newStart = mapping.get(currentChar) + 1;
// Removes these characters in the mapping.
for (int j = currentStart; j < newStart; j++) {
mapping.remove(s.charAt(j));
}
// Updates the starting point.
currentStart = newStart;
}
// Inserts the current character into the mapping.
mapping.put(currentChar, i);
// Checks whether the current substring becomes the longest substring.
if (i - currentStart + 1 > longest) {
longest = i - currentStart + 1;
}
}
return longest;
}
}