Advertisement
Guest User

SubstrW/ORepeats

a guest
Sep 24th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. """ Given a string, find the length of the longest substring without repeating characters.
  2.        Given "abcabcbb", the answer is "abc", which the length is 3.
  3.        Given "bbbbb", the answer is "b", with the length of 1.
  4.         Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence
  5.             and not a substring.
  6. """
  7.     def lengthOfLongestSubstring(self, s):
  8.         """
  9.        :type s: str
  10.        :rtype: int
  11.        """
  12.         """
  13.        strLen: Finds the length of a substring of s with no repeated characters
  14.        Input: s - The string to iterate over
  15.        """
  16.         def substrLen(s):
  17.             letters = set()
  18.             for l in s:
  19.                 if not l in letters:
  20.                     letters.add(l)
  21.                 else:
  22.                     break
  23.            
  24.             return len(letters)
  25.  
  26.         #Return the maximum of all recursive calls:
  27.         max = 0
  28.         for i in range(len(s)):
  29.             #Look through all possible starting locations
  30.             curr = substrLen(s[i:])
  31.             if curr > max:
  32.                 max = curr
  33.                
  34.         return max
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement