-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_substring.py
More file actions
32 lines (24 loc) · 1012 Bytes
/
longest_substring.py
File metadata and controls
32 lines (24 loc) · 1012 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
28
29
30
31
32
# -*- coding: utf-8 -*-
"""Longest Substring
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1b3bN-V-Vfhn0q4t72EeU4SVjzU5IcxUL
"""
def longest_substring_length(s):
# Dictionary to store the index of the last occurrence of each character
last_occurrence = {}
# Variables to keep track of the start of the current substring and the maximum length
start = 0
max_length = 0
for i, char in enumerate(s):
if char in last_occurrence and last_occurrence[char] >= start:
# If the character is repeated, update the start of the substring
start = last_occurrence[char] + 1
# Update the last occurrence index of the character
last_occurrence[char] = i
# Update the maximum length if the current substring is longer
max_length = max(max_length, i - start + 1)
return max_length
inputVal = input()
outputVal = longest_substring_length(inputVal)
print(outputVal)