Perfect! Let’s analyze LeetCode #3 — Longest Substring Without Repeating Characters using the Algorithm / Data Structure / Variable pattern.
🌳 Longest Substring Without Repeating Characters — Three-Level Analysis
Problem Statement
Given a string
s, find the length of the longest substring without repeating characters.
🧠 1. Algorithm Level — Logic and Rules
Goal: Find the maximum length substring with all unique characters.
Steps (Sliding Window Approach)
- Use two pointers
leftandrightto define a window of characters. - Expand
rightto include new characters. - If a character is repeated within the window, move
leftforward until the window is unique. - Track the maximum window size encountered.
Key Idea
- Sliding window ensures O(n) time complexity.
- Greedy: expand as long as substring is unique; shrink when repetition occurs.
C++ Code
int lengthOfLongestSubstring(string s) {
unordered_map<char, int> mp; // char -> last index
int left = 0, maxLen = 0;
for (int right = 0; right < s.size(); right++) {
if (mp.count(s[right]) && mp[s[right]] >= left) {
left = mp[s[right]] + 1; // move left past repeated char
}
mp[s[right]] = right; // update last index
maxLen = max(maxLen, right - left + 1);
}
return maxLen;
}
📦 2. Data Structure Level — What Actually Changes
| Data Structure | Purpose | How it Changes |
|---|---|---|
Hash Map mp | Tracks last index of each character | Updated as we encounter each character: mp[s[right]] = right |
String s | Input data | Read-only, used to check characters |
| Window | Conceptual substring [left, right] | Moves dynamically as left and right pointers change |
Example Trace (for s = "abcabcbb"):
Step 0: left=0, right=0, mp={}, maxLen=0
Step 1: s[0]='a', mp={} → mp={'a':0}, maxLen=1
Step 2: s[1]='b', mp={'a':0} → mp={'a':0,'b':1}, maxLen=2
Step 3: s[2]='c', mp={'a':0,'b':1} → mp={'a':0,'b':1,'c':2}, maxLen=3
Step 4: s[3]='a', repeated → move left=mp['a']+1=1, update mp['a']=3, maxLen=3
...
🧮 3. Variable Level — How Memory & Values Are Manipulated
| Variable | Role | Example / Update |
|---|---|---|
left | Start of window | Moves forward when repetition detected (left = mp[s[right]] + 1) |
right | End of window | Iterates over string (for right=0..s.size()-1) |
maxLen | Max length | Updated each step: maxLen = max(maxLen, right-left+1) |
mp | Stores last occurrence | mp[s[right]] = right |
- Variables are temporary storage and indices used to manipulate the window and hash map.
- Hash map updates are a CRUD operation: insert new character or update last index.
🧩 Summary Table
| Level | Focus | C++ Concept | CRUD / Operation | Example |
|---|---|---|---|---|
| Algorithm | Sliding window logic | Loop, pointer update | Read & update substring | Expand right, shrink left on repeat |
| Data Structure | Track character positions | unordered_map<char,int> | Insert / Update / Find | mp[s[right]] = right |
| Variable | Temporary computation | left, right, maxLen | Compute window size & indices | left = mp[s[right]]+1, maxLen = max(maxLen, right-left+1) |
✅ Key Insight
- Algorithm defines what to do (slide window, handle repeats).
- Data structures track state (map of last seen indices).
- Variables store temporary values and control window movement (mechanics of the algorithm).
Perfect! Let’s create a step-by-step table showing how the sliding window, hash map, and variables evolve in LeetCode #3 — Longest Substring Without Repeating Characters.
We’ll use this example:
s = "abcabcbb"
🌳 Longest Substring Without Repeating Characters — Step-by-Step
| Step | right | s[right] | left | Hash Map mp (char → last index) | maxLen | Action / Notes |
|---|---|---|---|---|---|---|
| 0 | 0 | ‘a’ | 0 | {} → {‘a’:0} | 1 | No repetition, expand window |
| 1 | 1 | ‘b’ | 0 | {‘a’:0} → {‘a’:0,‘b’:1} | 2 | No repetition, expand window |
| 2 | 2 | ‘c’ | 0 | {‘a’:0,‘b’:1} → {‘a’:0,‘b’:1,‘c’:2} | 3 | No repetition, expand window |
| 3 | 3 | ‘a’ | 0 → 1 | {‘a’:0,‘b’:1,‘c’:2} → {‘a’:3,‘b’:1,‘c’:2} | 3 | ‘a’ repeated → move left past last ‘a’ |
| 4 | 4 | ‘b’ | 1 → 2 | {‘a’:3,‘b’:1,‘c’:2} → {‘a’:3,‘b’:4,‘c’:2} | 3 | ‘b’ repeated → move left past last ‘b’ |
| 5 | 5 | ‘c’ | 2 → 3 | {‘a’:3,‘b’:4,‘c’:2} → {‘a’:3,‘b’:4,‘c’:5} | 3 | ‘c’ repeated → move left past last ‘c’ |
| 6 | 6 | ‘b’ | 4 → 5 | {‘a’:3,‘b’:4,‘c’:5} → {‘a’:3,‘b’:6,‘c’:5} | 3 | ‘b’ repeated → move left past last ‘b’ |
| 7 | 7 | ‘b’ | 6 → 7 | {‘a’:3,‘b’:6,‘c’:5} → {‘a’:3,‘b’:7,‘c’:5} | 3 | ‘b’ repeated → move left past last ‘b’ |
🔹 Explanation
rightiterates over the string.leftadjusts whenever a repeated character is detected in the current window.mpkeeps track of the last index of each character.maxLenis updated if the current window (right-left+1) is larger than the previous max.
🔹 Algorithm / Data Structure / Variable Perspective
| Level | Focus | Example from Table |
|---|---|---|
| Algorithm | Sliding window logic, repeat handling | Expand right, shrink left when a repeat is found |
| Data Structure | State tracking of characters | unordered_map<char,int> updated with last seen indices |
| Variable | Temporary computation / indices | left, right, maxLen |
✅ Key Insight:
- Algorithm: defines the sliding window and repeat handling.
- Data Structure: hash map tracks characters dynamically (CRUD operations: insert/update/find).
- Variables: store pointers and temporary values to compute window size.


774

被折叠的 条评论
为什么被折叠?



