Skip to content

Commit 6311894

Browse files
Sean PrashadSean Prashad
authored andcommitted
Update 3_Longest_Substring_Without_Repeating_Characters.java
1 parent 70862f9 commit 6311894

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

Sliding Window/3_Longest_Substring_Without_Repeating_Characters.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ public int lengthOfLongestSubstring(String s) {
44
return 0;
55
}
66

7-
int start = 0, end = 0, maxLen = Integer.MIN_VALUE;
8-
Set<Character> set = new HashSet<>();
7+
Map<Character, Integer> map = new HashMap<>();
8+
int result = 0, j = 0;
99

10-
while (end < s.length()) {
11-
if (!set.contains(s.charAt(end))) {
12-
set.add(s.charAt(end++));
13-
maxLen = Math.max(maxLen, end - start);
14-
} else {
15-
set.remove(s.charAt(start++));
10+
for (int i = 0; i < s.length(); i++) {
11+
if (map.containsKey(s.charAt(i))) {
12+
j = Math.max(j, map.get(s.charAt(i)) + 1);
1613
}
14+
15+
result = Math.max(result, i - j + 1);
16+
map.put(s.charAt(i), i);
1717
}
1818

19-
return maxLen;
19+
return result;
2020
}
2121
}

0 commit comments

Comments
 (0)