Skip to content

Commit bbae71a

Browse files
author
Partho Biswas
committed
Longest substring without duplicate
1 parent 0bd3bbe commit bbae71a

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
Can take a look into my <a href="https://leetcode.com/parthobiswas007/">leetcode profile</a> if interested.
2222
</p>
2323

24-
<h4 align="center">Updated daily :) If it was helpful please press a star</h4>
24+
<h4 align="center">Show your support by giving it a STAR</h4>
2525

2626
<p align="center">
2727
<a href="#About">About</a> •
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
# O(n) time | O(min(n, a)) space
3+
def longestSubstringWithoutDuplication(string):
4+
lastSeen = {}
5+
longest = [0, 1] # Here 0 and 1 is the values in the 0 and 1 index. This variable stores the start and end index of of the longest string.
6+
startIndex = 0
7+
for i, char in enumerate(string):
8+
if char in lastSeen:
9+
startIndex = max(startIndex, lastSeen[char] + 1)
10+
if longest[1] - longest[0] < i + 1 - startIndex: # Here 0 and 1 is the index of the value of longest array
11+
longest = [startIndex, i + 1]
12+
lastSeen[char] = i
13+
return string[longest[0]:longest[1]]
14+
15+
16+

0 commit comments

Comments
 (0)