Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 15b0841

Browse files
committed
add 3 solution (medium)
1 parent 87ff2a6 commit 15b0841

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
![Screen Shot 2022-02-18 at 1.03.31 PM.png](images/3-1.png)
62+
63+
![Screen Shot 2022-02-18 at 1.03.50 PM.png](images/3-2.png)
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+
```

3.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def lengthOfLongestSubstring(self, s: str) -> int:
3+
# sliding window approach requiring two pointers
4+
# set variable containing all unique
5+
left = 0
6+
right = 0
7+
longest = 0
8+
charset = set()
9+
10+
for right in range(len(s)):
11+
while s[right] in charset:
12+
# look for duplicate
13+
charset.remove(s[left])
14+
left += 1
15+
16+
charset.add(s[right])
17+
if len(charset) > longest:
18+
longest = len(charset)
19+
20+
return longest
21+
22+
23+
if __name__ == '__main__':
24+
obj = Solution()
25+
obj.lengthOfLongestSubstring(s = "abcabcbb")
26+

images/3-1.png

130 KB
Loading

images/3-2.png

85 KB
Loading

0 commit comments

Comments
 (0)