Skip to the code
cf.
+25 XP

#142 Longest Repeat-Free Slice

medium

A password strength meter rewards long runs of characters with no repeats. Given the typed text s, it looks for the longest slice of neighboring characters in which no character appears twice.

Write longestRepeatFree(s) that returns the length of that slice. Upper and lower case letters are different characters.

Example 1

Input: s = "mapmaker"
Output: 6

pmaker is the longest slice without a repeat.

Example 2

Input: s = "kayak"
Output: 3

kay and yak both work. Any longer slice repeats a letter.

Constraints

  • 0 <= s.length <= 100000
  • s contains printable ASCII characters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).