← PayPal bank

PayPal · Question 16 · Strings

Longest Substring Without Repeating Characters

StringsJavacode⏱ O(n)💾 O(min(n,charsetsize))
Product-based

Problem

Find the length of the longest substring without repeating characters.

Approach

Sliding window with a HashSet to track characters.

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

How it works

  1. 1Expand right, tracking window characters in a set.
  2. 2Shrink left past repeats.
  3. 3Track the widest repeat-free window.

Sample input / output

Input:
s = "abcabcbb"
Output:
3

Constraints

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

Solution — read it, then walk it

Time O(n)Space O(min(n,charsetsize))Loops 2Returns 1source · Java
Scroll sideways for wide charts — the view follows the active step.
↻ repeat↻ repeatSTARTimport java.util.*DEFINE class MainDEFINE static intlengthOfLongestSubstring(Strings)Set<Character> set = newHashSet<>()DEFINE left = 0, maxLen = 0for (int right = 0;right < s.length();right++)while(set.contains(s.charAt(right)))set.remove(s.charAt(left))left++set.add(s.charAt(right))maxLen = Math.max(maxLen,right - left + 1)RETURN maxLenDEFINE static voidmain(String[] args)System.out.println(lengthOfLongestSubstring("abcabcbb"))END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/15

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