forked from rupashi97/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
substrLen.py
56 lines (40 loc) · 1.31 KB
/
substrLen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'''
Longest substring without repeating characters
Difference b/w substring and subsequence:
A Substring takes out characters from a string placed between two specified indices in a continuous order.
Subsequence can be derived from another sequence by deleting some or none of the elements in between but
always maintaining the relative order of elements in the original sequence.
'''
# O(n) solution | Done by myself
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
M = 1
sub = set()
left = 0
for x in s:
if x not in sub:
sub.add(x)
continue
m = len(sub)
M = max(M, m)
while x in sub:
sub.remove(s[left])
left += 1
sub.add(x)
if not s: return 0
return max(M, len(sub)) # if s=au for example
# O(n^2) | done by myself
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
res = 1
for i in range(len(s) - 1):
maxl = 1
sub = set({s[i]})
for j in range(i + 1, len(s)):
if s[j] in sub:
break
sub.add(s[j])
maxl += 1
res = max(res, maxl)
if not s: return 0
return res