Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f819a2b

Browse files
Create 13-00003-longest-substring0without-repeating-characters.py
1 parent 3c3a62a commit f819a2b

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Given a string s, find the length of the longest
2+
# substring
3+
# without repeating characters.
4+
5+
# Example 1:
6+
# Input: s = "abcabcbb"
7+
# Output: 3
8+
# Explanation: The answer is "abc", with the length of 3.
9+
10+
# Example 2:
11+
# Input: s = "bbbbb"
12+
# Output: 1
13+
# Explanation: The answer is "b", with the length of 1.
14+
15+
# Example 3:
16+
# Input: s = "pwwkew"
17+
# Output: 3
18+
# Explanation: The answer is "wke", with the length of 3.
19+
# Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
20+
21+
# Constraints:
22+
# 0 <= s.length <= 5 * 104
23+
# s consists of English letters, digits, symbols and spaces.
24+
25+
# -------------------------------------------------------------------------------------------------------------------------------------------------------------------
26+
27+
# SLIDING WINDOW:
28+
29+
# We can iterate through the given string with index r as the right boundary and l as the left boundary of the window.
30+
# We use a hash set to check if the character is present in the window or not.
31+
# When we encounter a character at index r that is already present in the window, we shrink the window by incrementing the l pointer until the window no longer contains any duplicates.
32+
# Also, we remove characters from the hash set that are excluded from the window as the l pointer moves.
33+
# At each iteration, we update the result with the length of the current window, r - l + 1, if this length is greater than the current result.
34+
35+
# maintain a substring between l and r pointers. Keep moving r to expand the substring, keep adding characters in the set
36+
# as soon as a duplicate is found, shrink the window from left until all characters in the substring are unique, keep removing characters in set
37+
# keep a variable to get the max length of the substring
38+
# return that variable
39+
40+
41+
42+
class Solution:
43+
def lengthOfLongestSubstring(self, s: str) -> int:
44+
45+
l = 0
46+
res = 0
47+
seen = set()
48+
49+
for r in range(len(s)):
50+
while s[r] in seen:
51+
seen.remove(s[l])
52+
l += 1
53+
54+
seen.add(s[r])
55+
res = max(res, (r - l) + 1)
56+
57+
return res
58+
59+
Time Complexity = O(n) where n is length of the string
60+
Space Complexity = O(m) where m is number of unique characters in the string
61+
62+
# Companies:
63+
# Google- 33
64+
# Amazon- 22
65+
# Microsoft- 14
66+
# Tiktok- 10
67+
# Bloomberg- 9
68+
# Meta- 8
69+
# Turing- 4
70+
# Goldman Sachs- 3
71+
# IBM- 2
72+
# Infosys- 2
73+
# Oracle- 11
74+
# Apple- 10
75+
# Adobe- 4
76+
# Zoho- 4
77+
# Nvidia- 4
78+
# Accenture- 4
79+
# Yandex- 4
80+
# EPAM Systems- 3
81+
# Juspay- 3
82+
# Tesla- 3
83+
# Yahoo- 23
84+
# Walmart Labs- 19
85+
# Uber- 15
86+
# Tinkoff- 14
87+
# Spotify- 10
88+
# Agoda- 8
89+
# Flipkart- 7
90+
# Bytedance- 7
91+
# JP Morgan- 6
92+
# eBay- 6

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /