Beyond Big O

Longest distinct substring

Longest distinct substring

Medium

Your task

  • Find the longest stretch of text with no repeated characters.
  • Return its length, not the text itself.
  • The stretch must be contiguous: you cannot skip characters as you would in a subsequence.
  • Return 0 for an empty string.

Required API

  • LongestDistinct
    func LongestDistinct(text string) int

Behavior

  • Matching is case-sensitive.
  • Spaces and punctuation count as ordinary characters; do not normalize or trim the text.

Constraints

  • The input is valid: 0–4,096 printable ASCII characters, with codes 32–126.
  • Separate calls must not share state.
  • Aim for O(n) time using only the standard library.
  • Explain the invariant your current window maintains.
Notes and example notation

Examples use {"text": "..."}.

Examples

The runner calls your functions directly; you do not need to parse JSON.

Example 1 · empty

Arguments
text
""
Expected return
0
Test data
Input
{"text":""}
Expected output
0

Example 2 · all distinct

Arguments
text
"abcd"
Expected return
4
Test data
Input
{"text":"abcd"}
Expected output
4

Example 3 · repeat inside

Arguments
text
"abcaef"
Expected return
5
Test data
Input
{"text":"abcaef"}
Expected output
5

Example 4 · all same

Arguments
text
"zzzzz"
Expected return
1
Test data
Input
{"text":"zzzzz"}
Expected output
1
Editor options
Typing preferences

Saved in this browser.

Restoring local drafts…