Skip to content

Commit eacfb6b

Browse files
committed
feat: longest-unique-substring challenge
1 parent 6a86fbf commit eacfb6b

File tree

1 file changed

+25
-0
lines changed
  • 2-NORMAL/Python/3-longest-unique-substring/proposed-solution

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Challenge: Given a string, find the length of the longest substring without repeating characters.
2+
# Use sliding window techniques to optimize the solution's performance.
3+
4+
def length_of_longest_substring(s):
5+
char_index_map = {}
6+
left = 0
7+
max_length = 0
8+
max_substring = ""
9+
10+
for right in range(len(s)):
11+
if s[right] in char_index_map and char_index_map[s[right]] >= left:
12+
left = char_index_map[s[right]] + 1
13+
char_index_map[s[right]] = right
14+
if right - left + 1 > max_length:
15+
max_length = right - left + 1
16+
max_substring = s[left:right+1]
17+
18+
return max_length, max_substring
19+
20+
# Example usage
21+
if __name__ == "__main__":
22+
test_string = "abcabcbb"
23+
length, substring = length_of_longest_substring(test_string)
24+
print(f"Length of longest substring without repeating characters: {length}")
25+
print(f"Longest substring without repeating characters: {substring}")

0 commit comments

Comments
 (0)