FastPrepLongest Substring Without Repeating Characters

Longest Substring Without Repeating Characters

TikTok logoTikTok● MediumINTERNPHONE SCREEN
Learn

Problem statement

Given a string text, return the length of its longest contiguous substring in which every character is distinct.

A substring keeps consecutive characters in their original order. Uppercase and lowercase letters are different characters, and spaces and punctuation also count. The empty string has answer 0.

Function

lengthOfLongestSubstring(text: String) → int

Examples

Example 1

text = "abcabcbb"return = 3

The substring abc has three distinct characters; no longer substring qualifies.

Example 2

text = "abba"return = 2

Either ab or ba has length two. The repeated letters cannot be skipped.

Example 3

text = ""return = 0

An empty string contains no nonempty substring.

Constraints

  • 0 <= text.length <= 10000.
  • Every character is printable ASCII, with code from 32 through 126.

More TikTok problems

See TikTok hiring insights
Saved
public int lengthOfLongestSubstring(String text) {
    // Write your code here.
}
text"abcabcbb"
expected3
Checking account…