Java String Program · S50
Longest Substring Without Repeating Characters in Java
Grow a window to the right while its characters stay unique. When a character repeats, move the left edge just past its previous occurrence.
Problem Statement
Given a string, return the length of the longest substring in which no character appears more than once.
A substring is contiguous. An empty string has length 0.
Input/Output Examples
"javabytechie"Output9Explanation: "vabytechi" has 9 distinct characters; adding the next e would repeat it."abcdefa"Output6Explanation: "abcdef" or "bcdefa"; both have 6 distinct characters."aaaa"Output1Explanation: every window of length 2 repeats a.Visual Explanation
- right = 2windowjav"jav" is unique best = 3
- right = 3windowjavaa seen at 1 left jumps to 2
- right = 10windowvabytechileft = 2, length 9 best = 9
- Donethe next e repeats9
"javabytechie": when the second a arrives (amber), the window jumps past the first a. The window "vabytechi" later reaches length 9.Storing the last index of every character lets the left edge jump directly past a repeat, instead of moving one step at a time. Each character is therefore visited by the right edge once.
Approaches
Both approaches keep a window of distinct characters. The first jumps the left edge; the second slides it one step at a time.
Approach 1: Sliding Window With Last Index (Recommended)
Idea: remember the last index of each character; if it lies inside the window, move left just past it.
- Initialise
last[c] = -1for every character. - For each
right, iflast[c] ≥ left, setleft = last[c] + 1. - Update
last[c] = rightand the best length.
Time: O(n) · Space: O(1). An array of 128 entries for ASCII.
Approach 2: Sliding Window With a HashSet
Idea: keep the window's characters in a set; while the new character is already in it, remove characters from the left.
- For each
right, while the set containss[right], removes[left]and moveleft. - Add
s[right]to the set. - Update the best length.
Time: O(n) · Space: O(k). Each character enters and leaves the set at most once.
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Sliding Window With Last Index (Recommended)
The check last[c] ≥ left ignores old occurrences that are already outside the window.
import java.util.Arrays;
public class UniqueWindowLastIndex {
// When a character repeats, jump the left edge just past its previous position
static int longestUnique(String s) {
int[] last = new int[128];
Arrays.fill(last, -1);
int best = 0;
int left = 0;
for (int right = 0; right < s.length(); right++) {
char c = s.charAt(right);
if (last[c] >= left) {
left = last[c] + 1;
}
last[c] = right;
best = Math.max(best, right - left + 1);
}
return best;
}
public static void main(String[] args) {
String[] inputs = {"javabytechie", "abcdefa", "aaaa", ""};
for (String s : inputs) {
System.out.println("\"" + s + "\" -> " + longestUnique(s));
}
}
}
"javabytechie" -> 9 "abcdefa" -> 6 "aaaa" -> 1 "" -> 0
Time complexity: O(n) · Space complexity: O(1).
Approach 2: Sliding Window With a HashSet
Works for any character set and is easy to explain, at the cost of a few more steps.
import java.util.HashSet;
import java.util.Set;
public class UniqueWindowSet {
// Shrinks the window one character at a time until the repeated character is gone
static int longestUnique(String s) {
Set<Character> window = new HashSet<>();
int best = 0;
int left = 0;
for (int right = 0; right < s.length(); right++) {
char c = s.charAt(right);
while (window.contains(c)) {
window.remove(s.charAt(left++));
}
window.add(c);
best = Math.max(best, right - left + 1);
}
return best;
}
public static void main(String[] args) {
String[] inputs = {"javabytechie", "abcdefa", "aaaa", ""};
for (String s : inputs) {
System.out.println("\"" + s + "\" -> " + longestUnique(s));
}
}
}
"javabytechie" -> 9 "abcdefa" -> 6 "aaaa" -> 1 "" -> 0
Time complexity: O(n) · Space complexity: O(k).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Sliding Window With Last Index | O(n) | O(1) |
| Sliding Window With a HashSet | O(n) | O(k) |
Here n is the length of the string and k the number of distinct characters. Both pointers only move forward, so the total work is linear.
Interview Tips and Recommended Approach
- Guard against old indexes. Without
last[c] ≥ left, the left edge could move backwards, as in"abba". - Explain why it is linear. The left pointer never moves back, so each character is added and removed at most once.
- Return the substring if asked. Remember the window start whenever the best length improves.
- Use an array for ASCII. It is faster than a
HashMap, which is needed only for larger alphabets.
Recommended Approach: Sliding Window With Last Index
Why: it moves the left edge directly to the right place, giving a single linear pass with a fixed-size array.