Unicorns VO OA Interview Question: Longest Substring Without Repeating Characters

13 Views
No Comments

Longest Substring Without Repeating Characters

Given a string, find the longest substring that does not contain any repeated characters.

Example 1:

Input: "abcabcbb"
Output: "abc"
Explanation: The longest substring without repeating characters is "abc".

Example 2:

Input: "bbbbb"
Output: "b"
Explanation: The longest substring without repeating characters is "b".

Example 3:

Input: "pwwkew"
Output: "wke"
Explanation: The longest substring without repeating characters is "wke".

Implement the following method:

class Solution {public String longestSubstring(String s) {}}

This problem asks for the longest substring without repeating characters. The standard solution uses a sliding window with a hash set or hash map to track characters in the current window. Expand the right pointer as long as characters remain unique, and move the left pointer forward when a duplicate appears. This gives an efficient linear-time approach and matches the sample cases such as fully unique, fully repeated, and mixed-character strings.

END
 0