File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ """
3+ - Time Complexity: O(n), n = len(s)
4+ - Space Complexity: O(n)
5+ """
6+ def lengthOfLongestSubstring (self , s : str ) -> int :
7+ check_set = set ()
8+
9+ longest_length , length = 0 , 0
10+ l , r = 0 , 0
11+ while r < len (s ):
12+ # check each character (s[r]) is duplicated, and expand or narrow the length
13+ if s [r ] not in check_set :
14+ check_set .add (s [r ])
15+ length += 1
16+ longest_length = max (longest_length , length )
17+ r += 1
18+ else :
19+ check_set .remove (s [l ])
20+ length -= 1
21+ l += 1
22+
23+ return longest_length
24+
25+ tc = [
26+ ("abcabcbb" , 3 ),
27+ ("bbbbb" , 1 ),
28+ ("pwwkew" , 3 )
29+ ]
30+
31+ sol = Solution ()
32+ for i , (s , e ) in enumerate (tc , 1 ):
33+ r = sol .lengthOfLongestSubstring (s )
34+ print (f"TC { i } is Passed!" if r == e else f"TC { i } is Failed! - Expected: { e } , Result: { r } " )
You can’t perform that action at this time.
0 commit comments