LeetCode #3 - Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, in the string 'abcabcbb', the answer is 3, with the substring being 'abc'.
Problem Statement
def length_of_longest_substring(s: str) -> int:
Example 1
Input: 'abcabcbb' Output: 3 Explanation: The longest substring is 'abc', which has length 3.
Example 2
Input: 'bbbbb' Output: 1 Explanation: The longest substring is 'b', with length 1.
Constraints
* 0 <= s.length <= 1000 * s consists of English letters, digits, symbols and spaces.
Structured Response
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <string>
using namespace std;
int longestUniqueSubstring(const string &s) {
unordered_map<char, int> charIndexMap;
int maxLength = 0;
int start = 0;
for (int i = 0; i < s.size(); i++) {
char charAtI = s[i];
if (charIndexMap.count(charAtI)) {
start = max(start, charIndexMap[charAtI] + 1);
}
charIndexMap[charAtI] = i;
maxLength = max(maxLength, i - start + 1);
}
return maxLength;
}
int main() {
string inputString = "abcabcbb";
cout << longestUniqueSubstring(inputString) << endl;
return 0;
}
package main
import (
"fmt"
)
func longestUniqueSubstring(s string) int {
charIndexMap := make(map[rune]int)
maxLength := 0
start := 0
for i, char := range s {
if lastIndex, exists := charIndexMap[char]; exists {
start = max(start, lastIndex+1)
}
charIndexMap[char] = i
maxLength = max(maxLength, i-start+1)
}
return maxLength
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func main() {
inputString := "abcabcbb"
fmt.Println(longestUniqueSubstring(inputString))
}
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
String inputString = "abcabcbb";
System.out.println(longestUniqueSubstring(inputString));
}
public static int longestUniqueSubstring(String s) {
HashMap<Character, Integer> charIndexMap = new HashMap<>();
int maxLength = 0;
int start = 0;
for (int i = 0; i < s.length(); i++) {
char charAtI = s.charAt(i);
if (charIndexMap.containsKey(charAtI)) {
start = Math.max(start, charIndexMap.get(charAtI) + 1);
}
charIndexMap.put(charAtI, i);
maxLength = Math.max(maxLength, i - start + 1);
}
return maxLength;
}
}
function longestUniqueSubstring(s) {
const charIndexMap = {};
let maxLength = 0;
let start = 0;
for (let i = 0; i < s.length; i++) {
const char = s[i];
if (charIndexMap[char] !== undefined) {
start = Math.max(start, charIndexMap[char] + 1);
}
charIndexMap[char] = i;
maxLength = Math.max(maxLength, i - start + 1);
}
return maxLength;
}
// Example usage:
const inputString = 'abcabcbb';
console.log(longestUniqueSubstring(inputString));
## Approach
Use a sliding window technique with two pointers. Maintain a hash map to store the last seen index of characters. If a character repeats, update the start pointer.
## Complexity
**Time:** O(n)
**Space:** O(min(n, m)) where m is the number of unique characters.
Share
Trusted by 100+ professionals preparing for interviews
Trusted by 100+ professionals 50+ Company Question Banks 5+ Supported Languages
Practice More Questions Like This
Generate unlimited interview questions with structured answers, code runner, and AI-powered walkthroughs.
Design a URL shortening service like Bitly. Consider functionality such as creating a short URL, redirecting to the original URL, and tracking analytics. Describe your approach to managing these features and which key components would be included in the architecture.
System Design · Mid-Level
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.