GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — algorithms/array/longest_non_repeat.py at master · RajaRamu/algorithms Expand file tree/ Copy pathlongest_non_repeat.pyMore file actions 27 lines (21 loc) · 746 Bytes / Copy pathlongest_non_repeat.pyFile metadata and controls27 lines (21 loc) · 746 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 # Given a string, find the length of the longest substring # without repeating characters. # Examples: # Given "abcabcbb", the answer is "abc", which the length is 3. # Given "bbbbb", the answer is "b", with the length of 1. # Given "pwwkew", the answer is "wke", with the length of 3. # Note that the answer must be a substring, # "pwke" is a subsequence and not a substring. def longest_non_repeat(s): start, maxlen = 0, 0 used_char = {} for i, char in enumerate(s): if char in used_char and start <= used_char[char]: start = used_char[char] + 1 else: maxlen = max(maxlen, i-start+1) used_char[char] = i return maxlen a = "abcabcdefbb" print(a) print(longest_non_repeat(a)) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |