Longest Substring Without Repeating Characters
Find the length of the longest substring without repeating characters using the sliding window and other approaches.
๐งฉ Problem Statement
Given a string s
, find the length of the longest substring without repeating characters.
โ๏ธ Function Signature
public int lengthOfLongestSubstring(String s)
๐ Approaches
โ 1. Sliding Window with HashSet (Two Pointers)
public int lengthOfLongestSubstring(String s) {
Set<Character> set = new HashSet<>();
int left = 0, maxLen = 0;
for (int right = 0; right < s.length(); right++) {
while (set.contains(s.charAt(right))) {
set.remove(s.charAt(left++));
}
set.add(s.charAt(right));
maxLen = Math.max(maxLen, right - left + 1);
}
return maxLen;
}
- Time Complexity: O(n)
- Space Complexity: O(k) (k = number of unique characters)
- ๐ Efficient and widely accepted approach
๐ง 2. Sliding Window with HashMap (Optimized Jumping)
public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> map = new HashMap<>();
int maxLen = 0;
for (int right = 0, left = 0; right < s.length(); right++) {
char c = s.charAt(right);
if (map.containsKey(c)) {
left = Math.max(map.get(c) + 1, left);
}
map.put(c, right);
maxLen = Math.max(maxLen, right - left + 1);
}
return maxLen;
}
- Time Complexity: O(n)
- Space Complexity: O(k)
- โก Optimized over Set-based window; avoids multiple character deletions
๐ 3. Brute Force (For Understanding Only)
public int lengthOfLongestSubstring(String s) {
int maxLen = 0;
for (int i = 0; i < s.length(); i++) {
Set<Character> set = new HashSet<>();
int j = i;
while (j < s.length() && !set.contains(s.charAt(j))) {
set.add(s.charAt(j++));
}
maxLen = Math.max(maxLen, j - i);
}
return maxLen;
}
- Time Complexity: O(nยฒ)
- Space Complexity: O(k)
- ๐ซ Not optimal for large inputs
๐งช Test Cases
public class LongestSubstringTest {
public static void main(String[] args) {
System.out.println(lengthOfLongestSubstring("abcabcbb")); // 3
System.out.println(lengthOfLongestSubstring("bbbbb")); // 1
System.out.println(lengthOfLongestSubstring("pwwkew")); // 3
System.out.println(lengthOfLongestSubstring("")); // 0
System.out.println(lengthOfLongestSubstring("dvdf")); // 3
}
public static int lengthOfLongestSubstring(String s) {
Set<Character> set = new HashSet<>();
int left = 0, maxLen = 0;
for (int right = 0; right < s.length(); right++) {
while (set.contains(s.charAt(right))) {
set.remove(s.charAt(left++));
}
set.add(s.charAt(right));
maxLen = Math.max(maxLen, right - left + 1);
}
return maxLen;
}
}
๐ Notes
- Use HashMap-based method for interviews โ avoids unnecessary operations
- Sliding window is efficient for large input strings
- Consider edge cases: empty string, all repeating characters, all unique characters
Related Posts
Java Interview Guide - Part 0: Core Questions (1โ50)
A comprehensive list of essential Java interview questions covering OOP, memory management, exceptions, collections, Streams, JDBC
Indepthdev-Backend-Team
Java Interview Guide - Part 3: Advanced Java Concepts (Q21โQ30)
A comprehensive list of essential Java interview questions covering OOP, memory management, exceptions, collections, and Streams
Indepthdev-Backend-Team
Java Interview Guide - Part 1: Core Questions (1โ10)
A comprehensive list of essential Java interview questions covering OOP, memory management, exceptions, collections
Indepthdev-Backend-Team