-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLongestSubstringWithoutRepeatingCharacters.java
33 lines (28 loc) · 1.56 KB
/
LongestSubstringWithoutRepeatingCharacters.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
package main.java.topcodingquestion.arraysandstrings;
import java.util.HashMap;
import java.util.Map;
public class LongestSubstringWithoutRepeatingCharacters {
public static int findLength(String str) {
int windowStart = 0, maxLength = 0;
Map<Character, Integer> charIndexMap = new HashMap<>();
// try to extend the range [windowStart, windowEnd]
for (int windowEnd = 0; windowEnd < str.length(); windowEnd++) {
char rightChar = str.charAt(windowEnd);
// if the map already contains the 'rightChar', shrink the window from the beginning so that
// we have only one occurrence of 'rightChar'
if (charIndexMap.containsKey(rightChar)) {
// this is tricky; in the current window, we will not have any 'rightChar' after its previous index
// and if 'windowStart' is already ahead of the last index of 'rightChar', we'll keep 'windowStart'
windowStart = Math.max(windowStart, charIndexMap.get(rightChar) + 1);
}
charIndexMap.put(rightChar, windowEnd); //insert the right char
maxLength = Math.max(maxLength, windowEnd - windowStart + 1);//Update the global max length
}
return maxLength;
}
public static void main(String[] args) {
System.out.println("Length of the longest substring: " + findLength("aabccbb"));
System.out.println("Length of the longest substring: " + findLength("abbbb"));
System.out.println("Length of the longest substring: " + findLength("abccde"));
}
}