Problem · String

Longest Non-Repeating Substring

Learn this problem
MediumCisco logoCiscoFULLTIMEPHONE SCREEN
See Cisco hiring insights

Problem statement

Given a string s, return a longest contiguous substring that contains no repeated characters.

For this exercise, assume that when several longest valid substrings exist, you must return the one with the smallest starting index. If s is empty, return the empty string.

Function

longestUniqueSubstring(s: String) → String

Examples

Example 1

s = "abcabcbb"return = "abc"

The longest valid substrings have length 3. The earliest one is "abc", which starts at index 0.

Example 2

s = "pwwkew"return = "wke"

Both "wke" and "kew" have length 3. The leftmost tie rule selects "wke".

Example 3

s = "bbbbb"return = "b"

Every valid substring has length 1, so the first character is returned.

Example 4

s = ""return = ""

The empty string has no non-empty substring, so return "".

Constraints

  • 0 <= s.length <= 200000
  • s contains lowercase English letters.

More Cisco problems

drafts saved locally
public String longestUniqueSubstring(String s) {
    // Write your code here.
}
s"abcabcbb"
expected"abc"
checking account