|
| 1 | +# 3 - Longest Substring Without Repeating Characters |
| 2 | + |
| 3 | +Difficulty: medium |
| 4 | +Done: Yes |
| 5 | +Last edited: February 18, 2022 2:06 PM |
| 6 | +Link: https://leetcode.com/problems/longest-substring-without-repeating-characters/ |
| 7 | +Topic: set, sliding window |
| 8 | + |
| 9 | +## Problem |
| 10 | + |
| 11 | +Given a string `s`, find the length of the **longest substring** without repeating characters. |
| 12 | + |
| 13 | +Example 1: |
| 14 | + |
| 15 | +``` |
| 16 | +Input: s = "abcabcbb" |
| 17 | +Output: 3 |
| 18 | +Explanation: The answer is "abc", with the length of 3. |
| 19 | +
|
| 20 | +``` |
| 21 | + |
| 22 | +Example 2 |
| 23 | + |
| 24 | +``` |
| 25 | +Input: s = "bbbbb" |
| 26 | +Output: 1 |
| 27 | +Explanation: The answer is "b", with the length of 1. |
| 28 | +
|
| 29 | +``` |
| 30 | + |
| 31 | +Example 3 |
| 32 | + |
| 33 | +``` |
| 34 | +Input: s = "pwwkew" |
| 35 | +Output: 3 |
| 36 | +Explanation: The answer is "wke", with the length of 3. |
| 37 | +Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. |
| 38 | +
|
| 39 | +``` |
| 40 | + |
| 41 | +Constraints |
| 42 | + |
| 43 | +- `0 <= s.length <= 5 * 104` |
| 44 | +- `s` consists of English letters, digits, symbols and spaces. |
| 45 | + |
| 46 | +## Solution |
| 47 | + |
| 48 | +Intuitively I was attempting to use a hashtable, and buffer variable. Looping through array and adding current element to buffer, stoping when we reach a duplicate insider our buffer, then copying the buffer into our hashtable. However, it became complicated as far as string manipulation. |
| 49 | + |
| 50 | +A better approach is to use a **sliding window** algorithm with two pointers, and a set which allows for unique elements. Found algorithm to be very similar to my initial approach, with the exception that using sliding window we easily remove the left-most duplicate element in the current window. |
| 51 | + |
| 52 | +Then the size of the window at the largest point can be used to return to the largest substring, through each iteration we compare the current size of the set to the *longest* variable and overwrite if necessary |
| 53 | + |
| 54 | +```python |
| 55 | +if len(charset) > longest: |
| 56 | +longest = len(charset) |
| 57 | +``` |
| 58 | + |
| 59 | +## Whiteboard |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +## Code |
| 66 | + |
| 67 | +```python |
| 68 | +buffer = [] |
| 69 | + subs = [] |
| 70 | + |
| 71 | + if len(s) == 1: |
| 72 | + return 1 |
| 73 | + elif len(s) == 0: |
| 74 | + return 0 |
| 75 | + |
| 76 | + for i in range(len(s)): |
| 77 | + if s[i] not in buffer: |
| 78 | + buffer.append(s[i]) |
| 79 | + else: |
| 80 | + subs.append(''.join(buffer)) |
| 81 | + buffer.clear() |
| 82 | + buffer.append(s[i]) |
| 83 | + |
| 84 | + subs.append(''.join(buffer)) |
| 85 | + |
| 86 | + return len(max(subs, key=len)) |
| 87 | +``` |
| 88 | + |
| 89 | +```python |
| 90 | +class Solution: |
| 91 | + def lengthOfLongestSubstring(self, s: str) -> int: |
| 92 | + # sliding window approach requiring two pointers |
| 93 | + # use set to hold current window values |
| 94 | + left = 0 |
| 95 | + right = 0 |
| 96 | + longest = 0 |
| 97 | + charset = set() |
| 98 | + |
| 99 | + for right in range(len(s)): |
| 100 | + while s[right] in charset: |
| 101 | + charset.remove(s[left]) |
| 102 | + left += 1 |
| 103 | + |
| 104 | + charset.add(s[right]) |
| 105 | + |
| 106 | + if len(charset) > longest: |
| 107 | + # current set is larger than longest, overwrite |
| 108 | + # eventually longest will be hold size of largest charset |
| 109 | + longest = len(charset) |
| 110 | + |
| 111 | + return longest |
| 112 | +``` |
0 commit comments