Longest Substring With All Unique Characters
Solve this Problems, return the length of the longest substring that contains no repeated characters.
Checking every starting index and re-scanning forward works, but it forgets everything the previous start already learned about the string. The sliding windowSliding WindowMaintaining a running result over a contiguous range that grows or shrinks one element at a time, instead of recomputing the result for every range from scratch. technique keeps a hash map of each character's most recent index: grow the window by moving the right edge forward, and whenever a character repeats inside the current window, jump the left edge straight past that character's earlier occurrence — no need to shrink one step at a time. Every character is visited once by the right pointer, so the whole scan runs in O(n).
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
0 ≤ s.length ≤ 5 × 10⁴ - ◆
s consists of English letters, digits, symbols, and spaces
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
| 1 | class Solution { |
| 2 | public int lengthOfLongestUniqueSubstring(String s) { |
| 3 | Map<Character, Integer> lastSeen = new HashMap<>(); |
| 4 | int left = 0, maxLen = 0; |
| 5 | for (int right = 0; right < s.length(); right++) { |
| 6 | char c = s.charAt(right); |
| 7 | if (lastSeen.containsKey(c) && lastSeen.get(c) >= left) { |
| 8 | left = lastSeen.get(c) + 1; |
| 9 | } |
| 10 | lastSeen.put(c, right); |
| 11 | maxLen = Math.max(maxLen, right - left + 1); |
| 12 | } |
| 13 | return maxLen; |
| 14 | } |
| 15 | } |
| 16 |
00Start left at 0, maxLen at 0, and an empty map of each character's most recent index.
Approach & Solutions
Brute Force
BruteFor every possible starting index, grow the window to the right — tracking the characters seen so far in a set — until a repeat would enter, then record the window's length. Correct, but every start re-scans from scratch, throwing away everything the previous start already discovered about the string.
O(n²)O(n)1class Solution {
2 public int lengthOfLongestUniqueSubstring(String s) {
3 int maxLen = 0;
4 for (int i = 0; i < s.length(); i++) {
5 Set<Character> seen = new HashSet<>();
6 int j = i;
7 while (j < s.length() && !seen.contains(s.charAt(j))) {
8 seen.add(s.charAt(j));
9 j++;
10 }
11 maxLen = Math.max(maxLen, j - i);
12 }
13 return maxLen;
14 }
15}Optimal — Sliding Window
OptimalSlide a window right, tracking each character's most recent index in a hash map. When a character repeats and its last occurrence lies inside the current window, jump the left edge forward to just past that occurrence instead of shrinking one step at a time. Every character is visited once by the right pointer, so the whole scan runs in O(n).
O(n)O(min(n, charset))1class Solution {
2 public int lengthOfLongestUniqueSubstring(String s) {
3 Map<Character, Integer> lastSeen = new HashMap<>();
4 int left = 0, maxLen = 0;
5 for (int right = 0; right < s.length(); right++) {
6 char c = s.charAt(right);
7 if (lastSeen.containsKey(c) && lastSeen.get(c) >= left) {
8 left = lastSeen.get(c) + 1;
9 }
10 lastSeen.put(c, right);
11 maxLen = Math.max(maxLen, right - left + 1);
12 }
13 return maxLen;
14 }
15}