Longest substring without repeating characters
The problem
Given a string, S. Find the length of the longest substring without repeating characters.
Input : S = "abcddabac" Output : 4 Explanation : The answer is "abcd" , with a length of 4.
Input : S = "aaabbbccc" Output : 2 Explanation : The answers are "ab" , "bc". Both have maximum length 2.
Input : S = "aaaa"
- 1 <= S.length <= 5*104
- S contains only English lowercase letters.
cpp
class Solution{
public:
int longestNonRepeatingSubstring(string& s){
//your code goes here
}
};java
class Solution {
public int longestNonRepeatingSubstring(String s) {
//your code goes here
}
}python
class Solution:
def longestNonRepeatingSubstring(self, s):
#your code goes herejavascript
class Solution {
longestNonRepeatingSubstring(s) {
//your code goes here
}
}csharp
public class Solution
{
public int LongestNonRepeatingSubstring(string s)
{
//your code goes here
}
}go
func longestNonRepeatingSubstring(s string) int {
}Stuck? Show a way to structure it+
- 01Keep a left boundary and the latest index for each character.
- 02Extend the right boundary one character at a time.
- 03When a repeated character is inside the window, move left past its earlier index.
- 04Update the best window length after restoring uniqueness.
Reference answer
Then expect these follow-ups
How would you return the substring rather than its length?
Tests: window reconstruction
How does the approach change for Unicode grapheme clusters?
Tests: string representation
Hiring now
Quince has 52 open roles right now
Also hiring: Meesho
landed applies to roles like these for you, on each company’s own site, with answers written for the role.
Software Development Engineer III (Mobile)
Bengaluru, Karnataka, India
Software Engineer III, Frontend (Storefront)
Palo Alto, California, United States
Software Development Engineer - 3 (Backend)
Bengaluru, Karnataka, India
Next question