Given a string of lowercase English letters, find the length of its longest contiguous substring that doesn’t contain any character more than once.


For s = "abcabba", the output should be
uniqueChars(s) = 3.

The longest substring with unique characters is abc, so the answer should be 3.


  • [time limit] 4000ms (py)
  • [input] string sA string containing only lowercase English letters.Constraints:
    3 ≤ s.length ≤ 100.
  • [output] integer
def uniqueChars(s):
  longest = 0
  for i in range(len(s)):
    for j in range(i,len(s)):
      if len(s[i:j]) == len(set(s[i:j])) and j-i > longest:
        longest = j - i
  return longest

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s