← L&T Mindtree bank

L&T Mindtree · Question 12 · Strings

Longest Substring Without Repeating Characters

StringsJavacode⏱ O(n)💾 O(min(m,n))
Service-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. 2Jump the window start past in-window repeats.

Sample input / output

Input:
s = "pwwkew"
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 1Returns 1source · Java
Scroll sideways for wide charts — the view follows the active step.
yesno↻ repeatSTARTimport java.util.*DEFINE class MainDEFINE static intlengthOfLongestSubstring(Strings)Map<Character, Integer> mp= new HashMap<>()DEFINE maxLen = 0, start =0for (int end = 0; end< s.length(); end++)DEFINE c = s.charAt(end)if (mp.containsKey(c))start = Math.max(start,mp.get(c) + 1)mp.put(c, end)maxLen = Math.max(maxLen,end - start + 1)RETURN maxLenDEFINE static voidmain(String[] args)System.out.println(lengthOfLongestSubstring("pwwkew"))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