Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

3. Longest Substring Without Repeating Characters

Medium35.8% Acceptance
Hash TableStringSliding Window
Asked by:
Amazon
ProblemHints (1)Solutions (6)VideosCompanies (21)Notes

Problem Statement

Given a string s, find the length of the longest substring without repeating characters.

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

Constraints:

  • 0 <= s.length <= 5 * 104
  • s consists of English letters, digits, symbols and spaces.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
M
Microsoft
F
Facebook
B
Bloomberg
A
Apple
+16

Approach

The key challenge in #3 Longest Substring Without Repeating Characters is efficiently tracking characters in a substring while ensuring none repeat. A brute-force approach would check every possible substring, but this leads to unnecessary repeated work and poor performance.

A more optimal strategy uses the Sliding Window technique combined with a Hash Table (such as a set or map). Maintain two pointers that represent the current window of characters. As you expand the window, track characters using a hash-based structure. If a duplicate character appears, move the left pointer forward until the substring becomes valid again.

This method ensures each character is processed at most twice, leading to an efficient O(n) time complexity. The extra space depends on the character set being stored in the hash structure, typically O(min(n, charset)). This pattern is common in substring and window-based interview problems.

Complexity

ApproachTime ComplexitySpace Complexity
Sliding Window with Hash TableO(n)O(min(n, charset))

Video Solution Available

NeetCode

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

Generate all possible substrings & check for each substring if it's valid and keep updating maxLen accordingly.

Ready to see the solutions?View Solutions

Solutions (6)

Sliding Window Approach

This approach uses a two-pointer technique (often called a sliding window) to efficiently find the longest substring without repeating characters. The idea is to keep moving a start pointer to the right, while expanding the end pointer, ensuring all characters between the two pointers are unique.

Time Complexity: O(n), where n is the length of the string. Each character is visited at most twice, once by the start pointer and once by the end pointer.
Space Complexity: O(1), since we use a fixed array of size 128 for ASCII characters.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <string.h>
3
4int lengthOfLongestSubstring(char * s){
5    int n = strlen(s

Explanation

The code uses an integer array index of size 128 to store the last index + 1 of each character. We iterate end over the string, and if the character has appeared before, we move the start pointer to the right of the previous index. Then, update the maxLen if the current substring length is greater than maxLen.

Video Solutions

Watch expert explanations and walkthroughs

Longest Substring Without Repeating Characters - Leetcode 3 - Python

NeetCode
6:46657,669 views

Asked By Companies

21 companies
A
Amazon
M
Microsoft
F
Facebook
B
Bloomberg
A
Apple
G
Google
S
Spotify
A
Adobe
U
Uber
V
VMware
Y
Yahoo
G
Goldman Sachs
O
Oracle
W
Walmart Global Tech
S
Salesforce
P
PayPal
S
Samsung
I
Intuit
Y
Yandex
Z
Zoho
J
JPMorgan

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Integer to RomanMedium
Roman to IntegerEasy
Letter Combinations of a Phone NumberMedium
Substring with Concatenation of All WordsHard
More similar problems

Related Topics

Hash TableStringSliding Window

Problem Stats

Acceptance Rate35.8%
DifficultyMedium
Companies21

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why is the sliding window technique useful for this problem?

The sliding window technique helps avoid recomputing substrings from scratch. By adjusting window boundaries dynamically and tracking characters with a hash table, you can efficiently maintain a valid substring while scanning the string once.

Is Longest Substring Without Repeating Characters asked in FAANG interviews?

Yes, this problem is commonly asked in FAANG and other top tech company interviews. It tests understanding of sliding window patterns, hash tables, and string manipulation, which are important interview fundamentals.

What data structure is best for Longest Substring Without Repeating Characters?

A hash-based data structure such as a HashSet or HashMap works best. It allows constant-time checks for whether a character already exists in the current substring window, which is essential for maintaining efficiency.

What is the optimal approach for Longest Substring Without Repeating Characters?

The optimal approach uses the sliding window technique with a hash table to track characters currently in the window. Two pointers expand and shrink the window while ensuring no duplicate characters remain. This allows the problem to be solved in linear time.

Previous Problem

Two Sum

Next Problem

Integer to Roman

)
;
6
int
maxLen
=
0
;
7
int
index
[
128
]
=
{
0
}
;
8
int
start
=
0
;
9
for
(
int
end
=
0
;
end
<
n
;
end
++
)
{
10
start
=
index
[
s
[
end
]
]
>
start
?
index
[
s
[
end
]
]
:
start
;
11
maxLen
=
(
end
-
start
+
1
)
>
maxLen
?
(
end
-
start
+
1
)
:
maxLen
;
12
index
[
s
[
end
]
]
=
end
+
1
;
13
}
14
return
maxLen
;
15
}
16
17
int
main
(
)
{
18
printf
(
"%d\n"
,
lengthOfLongestSubstring
(
"abcabcbb"
)
)
;
// Output: 3
19
printf
(
"%d\n"
,
lengthOfLongestSubstring
(
"bbbbb"
)
)
;
// Output: 1
20
printf
(
"%d\n"
,
lengthOfLongestSubstring
(
"pwwkew"
)
)
;
// Output: 3
21
return
0
;
22
}
23