algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

longest-substring-recursive.py (553B)


      1 def recurse(s, left, right, char_set, best):
      2     if right == len(s):
      3         return best
      4     if s[right] in char_set:
      5         char_set.remove(s[left])
      6         return recurse(s, left + 1, right, char_set, best)
      7     if (right - left) + 1 > best:
      8         char_set.add(s[right])
      9         return recurse(s, left, right + 1, char_set, best + 1)
     10     else:
     11         char_set.add(s[right])
     12         return recurse(s, left, right + 1, char_set, best)
     13 
     14 class Solution:
     15 
     16     def lengthOfLongestSubstring(self, s: str) -> int:
     17         return recurse(s, 0, 0, set(), 0)