Longest Substring Without Repeating Characters
LeetCode 3 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
On a repeat, l = max(l, last[ch] + 1).
நான் Longest Substring Without Repeating Characters (LeetCode 3) ஐ இப்படித் தீர்த்தேன் — Variable Window – Longest பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
Grow a window to the right. When the new character already appears inside the window, jump the left edge just past its previous occurrence. The window always holds distinct characters, so its size is a candidate answer.
Approach
- 1
seen maps character → last index.
- 2
If s[r] was seen, move l past it (never backwards).
- 3
Update the max length and seen[s[r]] = r.
நான் எப்படித் தீர்த்தேன்
Sliding window + last indexPythonO(n) time
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
seen = {}
l=0
r=0
max_length=0
while r<len(s):
if s[r] in seen:
l= max(seen.get(s[r],0)+1,l)
max_length = max(max_length,r-l+1)
seen[s[r]]=r
r+=1
return max_length
Longest Substring Without Repeating Characters — அடிக்கடி கேட்கப்படும் கேள்விகள்
Longest Substring Without Repeating Characters பின்னாலுள்ள intuition என்ன?
Grow a window to the right. When the new character already appears inside the window, jump the left edge just past its previous occurrence. The window always holds distinct characters, so its size is a candidate answer.
Longest Substring Without Repeating Characters (LeetCode 3) ஐ எப்படித் தீர்ப்பது?
1. seen maps character → last index. 2. If s[r] was seen, move l past it (never backwards). 3. Update the max length and seen[s[r]] = r.
Longest Substring Without Repeating Characters இன் time மற்றும் space complexity என்ன?
Time: O(n). Space: O(Σ). (Accepted approach: Sliding window + last index.)
Longest Substring Without Repeating Characters இல் பொதுவான தவறுகள் என்ன?
Use max() — an old occurrence before l must not drag l backwards ('abba').