← Meta (Facebook) bank

Meta (Facebook) · Question 12 · Strings

Longest Substring Without Repeating Characters

StringsPythoncode⏱ O(n)💾 O(min(m,n))
Product-based

Problem

You are given a string. Find the length of the longest substring without repeating characters.

Approach

Slide a window, jumping its start past each repeated character.

Time O(n)Space O(min(m,n))

How it works

  1. 1Remember each character's latest index.
  2. 2On a repeat inside the window, jump start forward.
  3. 3Track the widest repeat-free window.

Sample input / output

Input:
dvdf
Output:
3

Constraints

0 <= s.length <= 5 * 10^4.

Solution — read it, then walk it

Time O(n)Space O(min(m,n))Loops 1Branches 2Returns 1source · Python
Scroll sideways for wide charts — the view follows the active step.
yesno↻ repeatyesnoSTARTsmp = {}maxLen = 0start = 0for end inrange(len(s))if s[end] in mpstart = max(start,mp[s[end]] + 1)mp[s[end]] = endmaxLen = max(maxLen, end -start + 1)RETURN maxLenDEFINE main()import sysdata =sys.stdin.read().split()it = iter(data)s = next(it)print(lengthOfLongestSubstring(s))if __name__ =="__main__"main()END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/19

StartSTART

Interactive control-flow diagram and step-by-step code walkthrough.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits