Skip to content

Instantly share code, notes, and snippets.

@zhiweio
Created January 11, 2019 14:00
Show Gist options
  • Select an option

  • Save zhiweio/d33c7471f188ca39d5aeb0dabba7237f to your computer and use it in GitHub Desktop.

Select an option

Save zhiweio/d33c7471f188ca39d5aeb0dabba7237f to your computer and use it in GitHub Desktop.
最长非重复子字串
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def lsubstring(s):
start = max_len = 0
seen = {}
for i, c in enumerate(s):
if start <= seen.get(c, -1):
start = seen[c] + 1
else:
max_len = max(max_len, i - start + 1)
seen[c] = i
sub = ''.join(seen.keys())
return max_len, sub
if __name__ == "__main__":
a = 'cbacbaa'
b = 'abcabca'
n, s = lsubstring(a)
print(n, s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment