I stopped brute-forcing LeetCode once I realized the difference A developer explains that shifting from brute-force to a sliding window approach, guided by a five-step AI workflow, dramatically improves LeetCode problem-solving efficiency, reducing time complexity from O(n^2) to O(n) for substring problems. The post includes code examples for finding the longest substring without repeating characters, highlighting the importance of tracking character indices to avoid backward pointer movement. I stopped brute-forcing LeetCode once I realized the difference The breakthrough came when I stopped writing code and started describing the problem in plain English: I just needed to find the longest stretch of characters where no letter appeared twice. That's when the sliding window concept clicked. Instead of resetting my search every time I hit a duplicate, I could just slide the left boundary of my "window" forward. I've since realized that most "hard" array or string problems can be cracked using a consistent five-step AI workflow for problem solving: 1. Isolate the core constraint e.g., what exactly makes a substring "invalid"? . 2. Pick a state-tracking structure usually a hash map or set for $O 1 $ lookups . 3. Set up two pointers a left and right boundary . 4. Expand and shrink move the right pointer to explore, and the left pointer to fix constraint violations . 5. Track the global optimum update your maximum or minimum result whenever the window is valid . To show the difference in performance, here is how the brute force approach fails compared to the optimized version. The inefficient way Brute Force $O n^2 $ : php def lengthOfLongestSubstring brute s: str - int: n = len s best = 0 for i in range n : seen = set for j in range i, n : if s j in seen: duplicate – stop this start position break seen.add s j best = max best, j - i + 1 return best The problem here is that the inner loop restarts the seen set for every single index, repeating massive amounts of work. If you're dealing with a string of $10^5$ characters, this will crawl. The optimized way Sliding Window $O n $ : php def lengthOfLongestSubstring s: str - int: """ Sliding window with a hash map storing the most recent index of each character. """ last index = {} char - latest position left = 0 start of the current window max len = 0 for right, ch in enumerate s : If ch was seen inside the current window, jump left just past its previous spot if ch in last index and last index ch = left: left = last index ch + 1 Update the most recent position of ch last index ch = right Window left, right is now valid max len = max max len, right - left + 1 return max len This approach is a total victory because last index allows us to jump the left pointer instantly. We never move backward, which guarantees linear time complexity. One huge gotcha: always remember the last index ch = left check. If you omit that, you might accidentally move your left pointer backward to a character that's already outside your current window, which breaks the whole logic. Next Dart 3. → /en/threads/6335/