{"slug": "i-stopped-brute-forcing-leetcode-once-i-realized-the-difference", "title": "I stopped brute-forcing LeetCode once I realized the difference", "summary": "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.", "body_md": "# I stopped brute-forcing LeetCode once I realized the difference\n\nThe 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.\n\nI've since realized that most \"hard\" array or string problems can be cracked using a consistent five-step AI workflow for problem solving:\n\n1. **Isolate the core constraint** (e.g., what exactly makes a substring \"invalid\"?).\n\n2. **Pick a state-tracking structure** (usually a hash map or set for $O(1)$ lookups).\n\n3. **Set up two pointers** (a left and right boundary).\n\n4. **Expand and shrink** (move the right pointer to explore, and the left pointer to fix constraint violations).\n\n5. **Track the global optimum** (update your maximum or minimum result whenever the window is valid).\n\nTo show the difference in performance, here is how the brute force approach fails compared to the optimized version.\n\n**The inefficient way (Brute Force $O(n^2)$):**\n\n``` php\ndef lengthOfLongestSubstring_brute(s: str) -> int:\n    n = len(s)\n    best = 0\n    for i in range(n):\n        seen = set()\n        for j in range(i, n):\n            if s[j] in seen: # duplicate – stop this start position\n                break\n            seen.add(s[j])\n            best = max(best, j - i + 1)\n    return best\n```\n\nThe problem here is that the inner loop restarts the `seen`\n\nset for every single index, repeating massive amounts of work. If you're dealing with a string of $10^5$ characters, this will crawl.\n\n**The optimized way (Sliding Window $O(n)$):**\n\n``` php\ndef lengthOfLongestSubstring(s: str) -> int:\n    \"\"\"\n    Sliding window with a hash map storing the most recent index of each character.\n    \"\"\"\n    last_index = {} # char -> latest position\n    left = 0 # start of the current window\n    max_len = 0\n\n    for right, ch in enumerate(s):\n        # If ch was seen inside the current window, jump left just past its previous spot\n        if ch in last_index and last_index[ch] >= left:\n            left = last_index[ch] + 1\n\n        # Update the most recent position of ch\n        last_index[ch] = right\n\n        # Window [left, right] is now valid\n        max_len = max(max_len, right - left + 1)\n\n    return max_len\n```\n\nThis approach is a total victory because `last_index`\n\nallows us to jump the `left`\n\npointer instantly. We never move backward, which guarantees linear time complexity. One huge gotcha: always remember the `last_index[ch] >= left`\n\ncheck. 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.\n\n[Next Dart 3. →](/en/threads/6335/)", "url": "https://wpnews.pro/news/i-stopped-brute-forcing-leetcode-once-i-realized-the-difference", "canonical_source": "https://promptcube3.com/en/threads/6336/", "published_at": "2026-08-14 22:56:25+00:00", "updated_at": "2026-08-14 23:13:33.793233+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["LeetCode", "Dart 3"], "alternates": {"html": "https://wpnews.pro/news/i-stopped-brute-forcing-leetcode-once-i-realized-the-difference", "markdown": "https://wpnews.pro/news/i-stopped-brute-forcing-leetcode-once-i-realized-the-difference.md", "text": "https://wpnews.pro/news/i-stopped-brute-forcing-leetcode-once-i-realized-the-difference.txt", "jsonld": "https://wpnews.pro/news/i-stopped-brute-forcing-leetcode-once-i-realized-the-difference.jsonld"}}