File tree Expand file tree Collapse file tree 5 files changed +44
-0
lines changed
Expand file tree Collapse file tree 5 files changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ test :
2+ python test_* .py
3+
4+ .PHONY : test
Original file line number Diff line number Diff line change 1+ # Longest Substring Without Repeating Characters
2+
3+ This problem was asked by Microsoft.
4+
5+ ## Description
6+
7+ Given a string, find the length of the longest substring without repeating characters.
8+
9+ ## Example
10+
11+ ```
12+ "abrkaabcdefghijjxxx" => 10
13+ ```
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def lengthOfLongestSubstring (self , s ):
3+ h = {}
4+ head , tail = 0 , - 1
5+ max_length = 0
6+
7+ while head < len (s ):
8+ if s [head ] in h :
9+ tail = max (tail , h [s [head ]])
10+
11+ h [s [head ]] = head
12+ max_length = max (max_length , head - tail )
13+ head += 1
14+
15+ return max_length
Original file line number Diff line number Diff line change 1+ import unittest
2+ from solver import Solution
3+
4+ class TestSolver (unittest .TestCase ):
5+ def test_solver (self ):
6+ self .assertEqual (Solution ().lengthOfLongestSubstring ("abrkaabcdefghijjxxx" ), 10 )
7+ self .assertEqual (Solution ().lengthOfLongestSubstring ("abcdefghij" ), 10 )
8+ self .assertEqual (Solution ().lengthOfLongestSubstring ("aaaaaaaaaa" ), 1 )
9+ self .assertEqual (Solution ().lengthOfLongestSubstring ("" ), 0 )
10+
11+ if __name__ == "__main__" :
12+ unittest .main ()
You can’t perform that action at this time.
0 commit comments