Created
January 11, 2019 14:00
-
-
Save zhiweio/d33c7471f188ca39d5aeb0dabba7237f to your computer and use it in GitHub Desktop.
最长非重复子字串
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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