This challenge here is to find the longest substring without repeating characters.
You can find the original problem described on LeetCode.
Given a string s
, find the length of the longest substring without repeating characters.
The problem definition on LeetCode also defines what is a substring: "A substring is a contiguous non-empty sequence of characters within a string".
Example 1
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Constraints
0 <= s.length <= 5 * 104
s
consists of English letters, digits, symbols and spaces.
I could think of three possible approaches one could use to solve this issue.
You can find a complete walkthrough of my solutions on my post.
Below is a table with a summary of the solutions.
Solutions | Time | Space |
---|---|---|
Brute Force | O(n²) |
O(min(m,n)) |
Sliding Window | O(n) |
O(min(m,n)) |
Optimized Sliding Window | O(n) |
O(1) |