GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — algorithms/array/longest_non_repeat.py at master · vspriya05/algorithms

Expand file tree

/

longest_non_repeat.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
27 lines (21 loc) · 746 Bytes
/

longest_non_repeat.py

Copy path

File metadata and controls

27 lines (21 loc) · 746 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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.