<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>DSA on Chirag Hasija</title>
    <link>https://chiraghasija.cc/dsa/</link>
    <description>Recent content in DSA on Chirag Hasija</description>
    <image>
      <title>Chirag Hasija</title>
      <url>https://chiraghasija.cc/og-image.png</url>
      <link>https://chiraghasija.cc/og-image.png</link>
    </image>
    <generator>Hugo -- 0.155.3</generator>
    <language>en-us</language>
    <atom:link href="https://chiraghasija.cc/dsa/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Best Time to Buy and Sell Stock</title>
      <link>https://chiraghasija.cc/dsa/best-time-to-buy-and-sell-stock/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/best-time-to-buy-and-sell-stock/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;prices = [7, 1, 5, 3, 6, 4]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;5&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Buy on day 1 (price = 1) and sell on day 4 (price = 6) for a profit of &lt;code&gt;6 - 1 = 5&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;You must buy before you sell, so scan left to right. At every position, the best you can do is sell at today&amp;rsquo;s price after buying at the cheapest price you&amp;rsquo;ve seen so far. By tracking the running minimum, you compute the optimal profit in a single pass.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Binary Tree Level Order Traversal</title>
      <link>https://chiraghasija.cc/dsa/binary-tree-level-order-traversal/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/binary-tree-level-order-traversal/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;        3
       / \
      9   20
         /  \
        15   7
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Output: &lt;code&gt;[[3], [9, 20], [15, 7]]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Level 0 has just the root &lt;code&gt;3&lt;/code&gt;. Level 1 has &lt;code&gt;9&lt;/code&gt; and &lt;code&gt;20&lt;/code&gt; (left to right). Level 2 has &lt;code&gt;15&lt;/code&gt; and &lt;code&gt;7&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;BFS naturally visits nodes level by level. The key trick is snapshotting the queue size at the start of each level — that tells you exactly how many nodes belong to the current depth. You process that many nodes, and any children you add during this pass belong to the next level.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Climbing Stairs</title>
      <link>https://chiraghasija.cc/dsa/climbing-stairs/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/climbing-stairs/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;n = 5&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;8&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;There are 8 distinct ways to climb 5 stairs using 1- or 2-steps: &lt;code&gt;[1,1,1,1,1]&lt;/code&gt;, &lt;code&gt;[2,1,1,1]&lt;/code&gt;, &lt;code&gt;[1,2,1,1]&lt;/code&gt;, &lt;code&gt;[1,1,2,1]&lt;/code&gt;, &lt;code&gt;[1,1,1,2]&lt;/code&gt;, &lt;code&gt;[2,2,1]&lt;/code&gt;, &lt;code&gt;[2,1,2]&lt;/code&gt;, &lt;code&gt;[1,2,2]&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;To reach step n, you must have come from either step n-1 (took 1 step) or step n-2 (took 2 steps). So the number of ways to reach step n is the sum of ways to reach those two previous steps. This is exactly the Fibonacci sequence, and you only need two variables instead of a full array.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Clone Graph</title>
      <link>https://chiraghasija.cc/dsa/clone-graph/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/clone-graph/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;Graph: &lt;code&gt;1 -- 2 -- 3 -- 1&lt;/code&gt; (a triangle where 1 connects to 2 and 3, and 2 connects to 3)&lt;/p&gt;
&lt;p&gt;Output: A new graph with the same structure &amp;ndash; node 1&amp;rsquo; connects to 2&amp;rsquo; and 3&amp;rsquo;, node 2&amp;rsquo; connects to 1&amp;rsquo; and 3&amp;rsquo;, node 3&amp;rsquo; connects to 1&amp;rsquo; and 2&amp;rsquo;. All nodes are new objects, not references to the originals.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;You need to create a copy of every node and replicate every edge, without getting stuck in cycles. The hashmap serves two purposes: it tracks which nodes have already been cloned (preventing infinite loops), and it lets you look up any node&amp;rsquo;s clone in O(1) when wiring up neighbor connections.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Coin Change</title>
      <link>https://chiraghasija.cc/dsa/coin-change/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/coin-change/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;coins = [1, 3, 4]&lt;/code&gt;, &lt;code&gt;amount = 6&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;2&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Because &lt;code&gt;3 + 3 = 6&lt;/code&gt;, which uses only 2 coins. Other combinations like &lt;code&gt;4 + 1 + 1&lt;/code&gt; use 3 coins, and &lt;code&gt;1 + 1 + 1 + 1 + 1 + 1&lt;/code&gt; uses 6.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;To make amount &lt;code&gt;i&lt;/code&gt; with the fewest coins, you can try adding one coin of each denomination to the best solution for &lt;code&gt;i - coin&lt;/code&gt;. By building up from amount 0, every smaller sub-problem is already solved when you need it, so you always pick the true minimum.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Combination Sum</title>
      <link>https://chiraghasija.cc/dsa/combination-sum/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/combination-sum/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;candidates = [2, 3, 6, 7]&lt;/code&gt;, &lt;code&gt;target = 7&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;[[2, 2, 3], [7]]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Two combinations sum to 7: &lt;code&gt;2 + 2 + 3 = 7&lt;/code&gt; and &lt;code&gt;7 = 7&lt;/code&gt;. Each candidate can be reused, so using 2 twice is allowed.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;You explore all possible combinations by trying each candidate and recursing with the updated total. Since candidates can be reused, the recursive call passes the same index &lt;code&gt;i&lt;/code&gt; (not &lt;code&gt;i+1&lt;/code&gt;). The start index prevents going backward, which eliminates duplicate orderings like [2,3] vs [3,2].&lt;/p&gt;</description>
    </item>
    <item>
      <title>Construct Binary Tree from Preorder and Inorder Traversal</title>
      <link>https://chiraghasija.cc/dsa/construct-binary-tree-from-preorder-and-inorder-traversal/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/construct-binary-tree-from-preorder-and-inorder-traversal/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;preorder = [3, 9, 20, 15, 7]&lt;/code&gt;, &lt;code&gt;inorder = [9, 3, 15, 20, 7]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;        3
       / \
      9   20
         /  \
        15   7
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Both traversals describe the same tree. The preorder tells us &lt;code&gt;3&lt;/code&gt; is the root, and the inorder tells us &lt;code&gt;9&lt;/code&gt; is in the left subtree while &lt;code&gt;20, 15, 7&lt;/code&gt; are in the right subtree.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Preorder traversal visits the root first, then left subtree, then right subtree. Inorder traversal visits left subtree, then root, then right subtree. So the first element in preorder tells you the root, and finding that root in inorder tells you how to split elements into &amp;ldquo;left subtree&amp;rdquo; and &amp;ldquo;right subtree&amp;rdquo; groups. Recursing on each group builds the full tree.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Container With Most Water</title>
      <link>https://chiraghasija.cc/dsa/container-with-most-water/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/container-with-most-water/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;height = [1, 8, 6, 2, 5, 4, 8, 3, 7]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;49&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The lines at index 1 (height 8) and index 8 (height 7) form the largest container. The water height is limited by the shorter line (7), and the width is &lt;code&gt;8 - 1 = 7&lt;/code&gt;, giving area &lt;code&gt;7 * 7 = 49&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Area = min(height[left], height[right]) * width. Starting with maximum width (pointers at both ends), you greedily move the shorter pointer inward. Moving the taller pointer can never increase the area because the bottleneck is the shorter side and the width is shrinking.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Contains Duplicate</title>
      <link>https://chiraghasija.cc/dsa/contains-duplicate/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/contains-duplicate/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;nums = [1, 2, 3, 1]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The value &lt;code&gt;1&lt;/code&gt; appears at both index 0 and index 3, so the array contains a duplicate.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;A hash set gives you O(1) lookups. As you walk through the array, you check whether each number has been seen before. The moment you find one that has, you know there is a duplicate &amp;ndash; no need to check the rest.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Course Schedule</title>
      <link>https://chiraghasija.cc/dsa/course-schedule/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/course-schedule/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;numCourses = 4&lt;/code&gt;, &lt;code&gt;prerequisites = [[1,0],[2,1],[3,2]]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Course 0 has no prerequisites, course 1 requires 0, course 2 requires 1, and course 3 requires 2. Taking them in order &lt;code&gt;0 -&amp;gt; 1 -&amp;gt; 2 -&amp;gt; 3&lt;/code&gt; satisfies all prerequisites with no circular dependencies.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Course prerequisites form a directed graph. If there is a cycle (A requires B, B requires C, C requires A), those courses can never be completed. Kahn&amp;rsquo;s algorithm detects cycles by peeling off nodes with no incoming edges layer by layer. If every node gets peeled off, there is no cycle and all courses are completable.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Daily Temperatures</title>
      <link>https://chiraghasija.cc/dsa/daily-temperatures/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/daily-temperatures/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;temperatures = [73, 74, 75, 71, 69, 72, 76, 73]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;[1, 1, 4, 2, 1, 1, 0, 0]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Day 0 (73) waits 1 day for 74. Day 2 (75) waits 4 days for 76. Day 3 (71) waits 2 days for 72. Days 6 and 7 have no warmer future day, so they get 0.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;A monotonic decreasing stack keeps track of days that haven&amp;rsquo;t found a warmer day yet. When a new warmer temperature comes along, it resolves all the cooler days sitting on the stack. Each index is pushed and popped at most once, so the total work is O(n) despite the nested loop.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Decode Ways</title>
      <link>https://chiraghasija.cc/dsa/decode-ways/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/decode-ways/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;s = &amp;quot;226&amp;quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;3&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The three valid decodings are: &lt;code&gt;&amp;quot;2|2|6&amp;quot;&lt;/code&gt; = BBF, &lt;code&gt;&amp;quot;22|6&amp;quot;&lt;/code&gt; = VF, and &lt;code&gt;&amp;quot;2|26&amp;quot;&lt;/code&gt; = BZ. Each split uses only valid letter mappings (1-26).&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;At each position you have at most two choices: decode the current digit as one letter (1-9 maps to A-I), or decode the current and previous digit together as one letter (10-26 maps to J-Z). The total decodings at position &lt;code&gt;i&lt;/code&gt; is the sum of decodings from both valid choices. Zeros can only appear as part of a two-digit code (10 or 20), never alone.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Evaluate Reverse Polish Notation</title>
      <link>https://chiraghasija.cc/dsa/evaluate-reverse-polish-notation/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/evaluate-reverse-polish-notation/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;tokens = [&amp;quot;2&amp;quot;, &amp;quot;1&amp;quot;, &amp;quot;+&amp;quot;, &amp;quot;3&amp;quot;, &amp;quot;*&amp;quot;]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;9&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;In standard notation this is &lt;code&gt;(2 + 1) * 3 = 9&lt;/code&gt;. The &lt;code&gt;+&lt;/code&gt; applies to 2 and 1 giving 3, then the &lt;code&gt;*&lt;/code&gt; applies to that 3 and 3 giving 9.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Reverse Polish Notation puts operators after their operands, so you never need parentheses or precedence rules. A stack naturally collects operands. When you hit an operator, the two most recent operands are right on top of the stack, ready to be combined.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Find Minimum in Rotated Sorted Array</title>
      <link>https://chiraghasija.cc/dsa/find-minimum-in-rotated-sorted-array/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/find-minimum-in-rotated-sorted-array/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;nums = [3, 4, 5, 1, 2]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;1&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The original sorted array &lt;code&gt;[1, 2, 3, 4, 5]&lt;/code&gt; was rotated 3 times to produce &lt;code&gt;[3, 4, 5, 1, 2]&lt;/code&gt;. The minimum element is still &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;In a rotated sorted array, the minimum is at the &amp;ldquo;rotation point&amp;rdquo; where the values drop. Comparing &lt;code&gt;nums[mid]&lt;/code&gt; to &lt;code&gt;nums[right]&lt;/code&gt; tells you which side the drop is on. If mid is larger than right, the drop (and minimum) must be somewhere to the right. Otherwise, the minimum is at mid or to its left.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Group Anagrams</title>
      <link>https://chiraghasija.cc/dsa/group-anagrams/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/group-anagrams/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;strs = [&amp;quot;eat&amp;quot;, &amp;quot;tea&amp;quot;, &amp;quot;tan&amp;quot;, &amp;quot;ate&amp;quot;, &amp;quot;nat&amp;quot;, &amp;quot;bat&amp;quot;]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;[[&amp;quot;eat&amp;quot;, &amp;quot;tea&amp;quot;, &amp;quot;ate&amp;quot;], [&amp;quot;tan&amp;quot;, &amp;quot;nat&amp;quot;], [&amp;quot;bat&amp;quot;]]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;eat&amp;rdquo;, &amp;ldquo;tea&amp;rdquo;, and &amp;ldquo;ate&amp;rdquo; are anagrams of each other (same letters rearranged). &amp;ldquo;tan&amp;rdquo; and &amp;ldquo;nat&amp;rdquo; are anagrams. &amp;ldquo;bat&amp;rdquo; has no anagram partner in the list.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;All anagrams are made of the exact same letters, just rearranged. If you sort each string alphabetically, anagrams collapse into the same string. Use that sorted string as a hashmap key to group them together.&lt;/p&gt;</description>
    </item>
    <item>
      <title>House Robber</title>
      <link>https://chiraghasija.cc/dsa/house-robber/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/house-robber/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;nums = [2, 7, 9, 3, 1]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;12&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Rob houses 0, 2, and 4 for a total of &lt;code&gt;2 + 9 + 1 = 12&lt;/code&gt;. No two of these houses are adjacent, and no other combination yields more.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;You can never rob two adjacent houses, so at each house you face a simple choice: rob it (adding its value to the best you could do two houses ago) or skip it (keeping the best from the previous house). By carrying forward just the last two best totals, you always have enough information to make the optimal decision.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Insert Interval</title>
      <link>https://chiraghasija.cc/dsa/insert-interval/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/insert-interval/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;intervals = [[1,3], [6,9]]&lt;/code&gt;, &lt;code&gt;newInterval = [2,5]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;[[1,5], [6,9]]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The new interval &lt;code&gt;[2,5]&lt;/code&gt; overlaps with &lt;code&gt;[1,3]&lt;/code&gt; (since 2 falls within 1-3), so they merge into &lt;code&gt;[1,5]&lt;/code&gt;. The interval &lt;code&gt;[6,9]&lt;/code&gt; does not overlap and stays unchanged.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Since the existing intervals are already sorted, you can process them in one pass with three distinct phases: intervals entirely before the new one, intervals overlapping with the new one (which get merged together), and intervals entirely after. This avoids re-sorting and handles all edge cases cleanly.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Invert Binary Tree</title>
      <link>https://chiraghasija.cc/dsa/invert-binary-tree/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/invert-binary-tree/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;Input:        4              Output:       4
            /   \                        /   \
           2     7                      7     2
          / \   / \                    / \   / \
         1   3 6   9                  9   6 3   1
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Every node&amp;rsquo;s left and right children are swapped, producing a mirror image of the original tree.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Every node in a mirrored tree has its left and right children swapped compared to the original. By swapping children at each node and recursing downward, every level of the tree gets mirrored. The recursion naturally handles all depths because each subtree is itself a smaller tree to invert.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Koko Eating Bananas</title>
      <link>https://chiraghasija.cc/dsa/koko-eating-bananas/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/koko-eating-bananas/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;piles = [3, 6, 7, 11]&lt;/code&gt;, &lt;code&gt;h = 8&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;4&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;At speed 4, Koko takes 1 hour for pile 3, 2 hours for pile 6, 2 hours for pile 7, and 3 hours for pile 11 &amp;ndash; totaling 8 hours, which exactly fits. Speed 3 would require 10 hours, which exceeds the limit.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Instead of searching through data, you are searching through possible answers (eating speeds 1 to max pile size). For each candidate speed, you can calculate exactly how many hours it takes. Since &amp;ldquo;can finish in time&amp;rdquo; flips from false to true at some speed, binary search finds that threshold efficiently.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Kth Smallest Element in a BST</title>
      <link>https://chiraghasija.cc/dsa/kth-smallest-element-in-a-bst/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/kth-smallest-element-in-a-bst/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;        5
       / \
      3   6
     / \
    2   4
   /
  1
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;k = 3&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;3&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The values in sorted order are &lt;code&gt;[1, 2, 3, 4, 5, 6]&lt;/code&gt;. The 3rd smallest is &lt;code&gt;3&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Inorder traversal of a BST always produces values in sorted ascending order (left, root, right). So the 1st node visited is the smallest, the 2nd is the next smallest, and so on. By counting visits and stopping at k, you find the kth smallest without sorting anything.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Linked List Cycle</title>
      <link>https://chiraghasija.cc/dsa/linked-list-cycle/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/linked-list-cycle/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;head = [3, 2, 0, -4]&lt;/code&gt; where the tail (-4) connects back to node with value 2&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;After reaching -4, the next pointer leads back to 2 instead of null, forming a loop: &lt;code&gt;2 -&amp;gt; 0 -&amp;gt; -4 -&amp;gt; 2 -&amp;gt; ...&lt;/code&gt;. Since the list never ends, it has a cycle.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Think of two runners on a circular track: the faster one will always lap the slower one. If the linked list has a cycle, the fast pointer (2 steps) will eventually catch up to the slow pointer (1 step) from behind. If there is no cycle, the fast pointer simply reaches the end.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Longest Increasing Subsequence</title>
      <link>https://chiraghasija.cc/dsa/longest-increasing-subsequence/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/longest-increasing-subsequence/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;nums = [10, 9, 2, 5, 3, 7, 101, 18]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;4&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;One longest increasing subsequence is &lt;code&gt;[2, 3, 7, 101]&lt;/code&gt;. Another is &lt;code&gt;[2, 5, 7, 101]&lt;/code&gt; or &lt;code&gt;[2, 3, 7, 18]&lt;/code&gt;. All have length 4, and no strictly increasing subsequence of length 5 exists.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;For each element, you ask: &amp;ldquo;What is the longest increasing subsequence I can build that ends right here?&amp;rdquo; You check every previous element &amp;ndash; if it is smaller, you could append the current element to that subsequence. Taking the maximum across all such extensions gives the best answer at each position.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Longest Repeating Character Replacement</title>
      <link>https://chiraghasija.cc/dsa/longest-repeating-character-replacement/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/longest-repeating-character-replacement/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;s = &amp;quot;AABABBA&amp;quot;&lt;/code&gt;, &lt;code&gt;k = 1&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;4&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The substring &lt;code&gt;&amp;quot;AABA&amp;quot;&lt;/code&gt; (indices 0-3) has 3 A&amp;rsquo;s and 1 B. Replacing that single B with A gives &lt;code&gt;&amp;quot;AAAA&amp;quot;&lt;/code&gt;, a string of 4 repeating characters, using exactly 1 replacement.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;In any window, you only need to replace the characters that are NOT the most frequent one. So the number of replacements needed is (window size - count of most frequent character). If that exceeds k, the window is too big and you shrink from the left. This lets you find the longest valid window in one pass.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Longest Substring Without Repeating Characters</title>
      <link>https://chiraghasija.cc/dsa/longest-substring-without-repeating-characters/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/longest-substring-without-repeating-characters/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;s = &amp;quot;abcabcbb&amp;quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;3&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The longest substring without any repeating characters is &lt;code&gt;&amp;quot;abc&amp;quot;&lt;/code&gt;, which has length 3. Other valid substrings like &lt;code&gt;&amp;quot;bca&amp;quot;&lt;/code&gt; or &lt;code&gt;&amp;quot;cab&amp;quot;&lt;/code&gt; also have length 3, but none longer exists without a repeated character.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;A sliding window lets you efficiently explore all substrings without repeating characters. Expand the right side to grow the window; when a duplicate appears, shrink from the left until it is gone. The set tracks what is currently in the window, giving O(1) duplicate checks.&lt;/p&gt;</description>
    </item>
    <item>
      <title>LRU Cache</title>
      <link>https://chiraghasija.cc/dsa/lru-cache/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/lru-cache/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;capacity = 2&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;put(1, 1)&lt;/code&gt;, &lt;code&gt;put(2, 2)&lt;/code&gt;, &lt;code&gt;get(1)&lt;/code&gt; returns &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;put(3, 3)&lt;/code&gt;, &lt;code&gt;get(2)&lt;/code&gt; returns &lt;code&gt;-1&lt;/code&gt;, &lt;code&gt;put(4, 4)&lt;/code&gt;, &lt;code&gt;get(1)&lt;/code&gt; returns &lt;code&gt;-1&lt;/code&gt;, &lt;code&gt;get(3)&lt;/code&gt; returns &lt;code&gt;3&lt;/code&gt;, &lt;code&gt;get(4)&lt;/code&gt; returns &lt;code&gt;4&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;After &lt;code&gt;put(1,1)&lt;/code&gt; and &lt;code&gt;put(2,2)&lt;/code&gt;, the cache is full. &lt;code&gt;get(1)&lt;/code&gt; returns &lt;code&gt;1&lt;/code&gt; and marks key 1 as recently used. When &lt;code&gt;put(3,3)&lt;/code&gt; is called, key 2 is evicted because it was used least recently, so &lt;code&gt;get(2)&lt;/code&gt; returns &lt;code&gt;-1&lt;/code&gt;. When &lt;code&gt;put(4,4)&lt;/code&gt; is called, key 1 is evicted, so &lt;code&gt;get(1)&lt;/code&gt; returns &lt;code&gt;-1&lt;/code&gt; while keys 3 and 4 are still accessible.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Maximum Depth of Binary Tree</title>
      <link>https://chiraghasija.cc/dsa/maximum-depth-of-binary-tree/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/maximum-depth-of-binary-tree/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;        3
       / \
      9   20
         /  \
        15   7
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Output: &lt;code&gt;3&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The longest path from root to leaf is 3 → 20 → 15 (or 3 → 20 → 7), which passes through 3 nodes.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;The depth of any node equals 1 (for itself) plus the depth of its deepest child. By recursing to the leaves where null returns 0, each call naturally computes the height of its subtree and passes it back up. The root ends up with the maximum depth of the entire tree.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Merge Intervals</title>
      <link>https://chiraghasija.cc/dsa/merge-intervals/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/merge-intervals/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;intervals = [[1,3], [2,6], [8,10], [15,18]]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;[[1,6], [8,10], [15,18]]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Intervals &lt;code&gt;[1,3]&lt;/code&gt; and &lt;code&gt;[2,6]&lt;/code&gt; overlap (they share the range 2-3), so they merge into &lt;code&gt;[1,6]&lt;/code&gt;. The remaining intervals &lt;code&gt;[8,10]&lt;/code&gt; and &lt;code&gt;[15,18]&lt;/code&gt; don&amp;rsquo;t overlap with anything, so they stay as-is.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Once intervals are sorted by start time, any overlapping intervals must be next to each other. You walk through the sorted list and either extend the current merged interval (if there is overlap) or start a new one (if there is a gap). This single pass handles all possible overlaps including chains of multiple intervals merging together.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Merge K Sorted Lists</title>
      <link>https://chiraghasija.cc/dsa/merge-k-sorted-lists/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/merge-k-sorted-lists/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;lists = [[1, 4, 5], [1, 3, 4], [2, 6]]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;[1, 1, 2, 3, 4, 4, 5, 6]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;All three sorted lists are combined into a single sorted list. Every element from every input list appears exactly once in the output, in non-decreasing order.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;You need the smallest element across k lists at each step. Comparing all k heads every time would be slow. A min-heap keeps the k heads organized so you can extract the minimum in O(log k) time instead of O(k), making the overall algorithm much faster.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Merge Two Sorted Lists</title>
      <link>https://chiraghasija.cc/dsa/merge-two-sorted-lists/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/merge-two-sorted-lists/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;list1 = [1, 3, 5]&lt;/code&gt;, &lt;code&gt;list2 = [2, 4, 6]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;[1, 2, 3, 4, 5, 6]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Both lists are already sorted individually. The merged result contains all elements from both lists arranged in non-decreasing order.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Both input lists are already sorted. At each step, you just compare the two front elements and take the smaller one. This is the same idea as the &amp;ldquo;merge&amp;rdquo; step in merge sort. By always choosing the smallest available node, the result list stays sorted.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Min Stack</title>
      <link>https://chiraghasija.cc/dsa/min-stack/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/min-stack/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;Operations: &lt;code&gt;push(-2)&lt;/code&gt;, &lt;code&gt;push(0)&lt;/code&gt;, &lt;code&gt;push(-3)&lt;/code&gt;, &lt;code&gt;getMin()&lt;/code&gt;, &lt;code&gt;pop()&lt;/code&gt;, &lt;code&gt;top()&lt;/code&gt;, &lt;code&gt;getMin()&lt;/code&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;push(-2)&lt;/code&gt;, &lt;code&gt;push(0)&lt;/code&gt;, &lt;code&gt;push(-3)&lt;/code&gt; &amp;ndash; stack now contains [-2, 0, -3]&lt;/li&gt;
&lt;li&gt;&lt;code&gt;getMin()&lt;/code&gt; returns &lt;code&gt;-3&lt;/code&gt; &amp;ndash; the smallest value currently in the stack&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pop()&lt;/code&gt; removes -3 from the top&lt;/li&gt;
&lt;li&gt;&lt;code&gt;top()&lt;/code&gt; returns &lt;code&gt;0&lt;/code&gt; &amp;ndash; the new top element&lt;/li&gt;
&lt;li&gt;&lt;code&gt;getMin()&lt;/code&gt; returns &lt;code&gt;-2&lt;/code&gt; &amp;ndash; with -3 removed, -2 is now the smallest&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;The trick is that the minimum can only change when you push or pop. By maintaining a second stack where each entry records &amp;ldquo;what is the minimum at this depth?&amp;rdquo;, you always know the current minimum by peeking at the top. When you pop, the previous minimum is automatically restored.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Minimum Window Substring</title>
      <link>https://chiraghasija.cc/dsa/minimum-window-substring/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/minimum-window-substring/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;s = &amp;quot;ADOBECODEBANC&amp;quot;&lt;/code&gt;, &lt;code&gt;t = &amp;quot;ABC&amp;quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;&amp;quot;BANC&amp;quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The substring &lt;code&gt;&amp;quot;BANC&amp;quot;&lt;/code&gt; (positions 9-12) is the shortest substring of &lt;code&gt;s&lt;/code&gt; that contains all three characters A, B, and C from &lt;code&gt;t&lt;/code&gt;. Other valid windows like &lt;code&gt;&amp;quot;ADOBEC&amp;quot;&lt;/code&gt; also contain all of them, but &lt;code&gt;&amp;quot;BANC&amp;quot;&lt;/code&gt; is shorter at length 4.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;You need the smallest window in &lt;code&gt;s&lt;/code&gt; that contains every character from &lt;code&gt;t&lt;/code&gt;. Expand the window rightward until it contains all required characters, then shrink from the left to minimize it. The &amp;ldquo;formed&amp;rdquo; counter tracks how many unique characters have met their required count, so you know exactly when the window is valid.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Non-overlapping Intervals</title>
      <link>https://chiraghasija.cc/dsa/non-overlapping-intervals/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/non-overlapping-intervals/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;intervals = [[1,2], [2,3], [3,4], [1,3]]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;1&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Removing just &lt;code&gt;[1,3]&lt;/code&gt; is enough to make the remaining intervals &lt;code&gt;[1,2]&lt;/code&gt;, &lt;code&gt;[2,3]&lt;/code&gt;, and &lt;code&gt;[3,4]&lt;/code&gt; non-overlapping. The interval &lt;code&gt;[1,3]&lt;/code&gt; overlaps with both &lt;code&gt;[1,2]&lt;/code&gt; and &lt;code&gt;[2,3]&lt;/code&gt;, so removing it solves all conflicts at once.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Sorting by end time is the key insight. By always keeping the interval that ends earliest, you leave the maximum room for future intervals. Any interval that overlaps with a kept interval must be removed &amp;ndash; and since we chose the earliest-ending one, we are guaranteed to minimize total removals. This is a classic greedy activity selection approach.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Number of Islands</title>
      <link>https://chiraghasija.cc/dsa/number-of-islands/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/number-of-islands/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;grid = [
  [&amp;#34;1&amp;#34;,&amp;#34;1&amp;#34;,&amp;#34;0&amp;#34;,&amp;#34;0&amp;#34;],
  [&amp;#34;1&amp;#34;,&amp;#34;0&amp;#34;,&amp;#34;0&amp;#34;,&amp;#34;1&amp;#34;],
  [&amp;#34;0&amp;#34;,&amp;#34;0&amp;#34;,&amp;#34;1&amp;#34;,&amp;#34;1&amp;#34;]
]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Output: &lt;code&gt;2&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;There are two separate groups of connected land cells. The first island consists of the three &lt;code&gt;1&lt;/code&gt;s in the upper-left corner, and the second island consists of the three &lt;code&gt;1&lt;/code&gt;s in the lower-right area. They are separated by water and not connected horizontally or vertically.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Each island is a connected group of &amp;lsquo;1&amp;rsquo; cells. When you find an unvisited &amp;lsquo;1&amp;rsquo;, you know it is a new island. By flood-filling (DFS) from that cell and turning every connected &amp;lsquo;1&amp;rsquo; into &amp;lsquo;0&amp;rsquo;, you &amp;ldquo;sink&amp;rdquo; the entire island so it will not be counted again. The total number of flood-fills you trigger equals the number of islands.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Pacific Atlantic Water Flow</title>
      <link>https://chiraghasija.cc/dsa/pacific-atlantic-water-flow/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/pacific-atlantic-water-flow/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;heights = [
  [1, 2, 2, 3, 5],
  [3, 2, 3, 4, 4],
  [2, 4, 5, 3, 1],
  [6, 7, 1, 4, 5],
  [5, 1, 1, 2, 4]
]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Output: &lt;code&gt;[[0,4], [1,3], [1,4], [2,2], [3,0], [3,1], [4,0]]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;For example, cell &lt;code&gt;(2,2)&lt;/code&gt; with height 5 can reach the Pacific (top/left edges) because water flows downhill through cells to the top or left, and can also reach the Atlantic (bottom/right edges) through cells to the bottom or right. Only these 7 cells can drain to both oceans.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Permutations</title>
      <link>https://chiraghasija.cc/dsa/permutations/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/permutations/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;nums = [1, 2, 3]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;[[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;There are 3! = 6 ways to arrange three distinct numbers. Each permutation uses every element exactly once in a different order.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;A permutation uses every element exactly once in some order. At each position, you try placing each unused element, then recurse to fill the remaining positions. The &lt;code&gt;used&lt;/code&gt; set prevents picking the same element twice. When all positions are filled, you have a complete permutation.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Product of Array Except Self</title>
      <link>https://chiraghasija.cc/dsa/product-of-array-except-self/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/product-of-array-except-self/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;nums = [1, 2, 3, 4]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;[24, 12, 8, 6]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Each position holds the product of all other elements: &lt;code&gt;result[0] = 2 * 3 * 4 = 24&lt;/code&gt;, &lt;code&gt;result[1] = 1 * 3 * 4 = 12&lt;/code&gt;, &lt;code&gt;result[2] = 1 * 2 * 4 = 8&lt;/code&gt;, &lt;code&gt;result[3] = 1 * 2 * 3 = 6&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;The product of all elements except index i equals (product of everything left of i) times (product of everything right of i). You can compute both with two simple passes, avoiding division entirely. The first pass builds left-products, and the second pass multiplies in right-products.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Remove Nth Node From End of List</title>
      <link>https://chiraghasija.cc/dsa/remove-nth-node-from-end-of-list/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/remove-nth-node-from-end-of-list/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;head = [1, 2, 3, 4, 5]&lt;/code&gt;, &lt;code&gt;n = 2&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;[1, 2, 3, 5]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The 2nd node from the end is &lt;code&gt;4&lt;/code&gt;. Removing it leaves the list as &lt;code&gt;1 → 2 → 3 → 5&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;If you put two pointers n+1 apart and slide them together, when the front pointer falls off the end, the back pointer is exactly one node before the one you want to remove. This finds the nth-from-end in a single pass without needing to know the list length.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Reverse Linked List</title>
      <link>https://chiraghasija.cc/dsa/reverse-linked-list/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/reverse-linked-list/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;head = [1, 2, 3, 4, 5]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;[5, 4, 3, 2, 1]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The original list &lt;code&gt;1 → 2 → 3 → 4 → 5&lt;/code&gt; is reversed so that every link points in the opposite direction, producing &lt;code&gt;5 → 4 → 3 → 2 → 1&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Each node in a linked list points to the next node. To reverse the list, you just need to make every node point to the previous node instead. By walking through one node at a time and flipping pointers as you go, you reverse the entire chain in a single pass.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Same Tree</title>
      <link>https://chiraghasija.cc/dsa/same-tree/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/same-tree/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;Tree p:     1        Tree q:     1
           / \                  / \
          2   3                2   3
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Output: &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Both trees have the same structure (root with two children) and identical values at every position: root is 1, left child is 2, right child is 3.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Two trees are identical only if every corresponding pair of nodes has the same value and the same structure. By checking three conditions at each step — both null, one null, or values differ — you catch every possible mismatch. Recursion handles the rest by drilling into left and right subtrees simultaneously.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Search in Rotated Sorted Array</title>
      <link>https://chiraghasija.cc/dsa/search-in-rotated-sorted-array/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/search-in-rotated-sorted-array/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;nums = [4, 5, 6, 7, 0, 1, 2]&lt;/code&gt;, &lt;code&gt;target = 0&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;4&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The original sorted array &lt;code&gt;[0, 1, 2, 4, 5, 6, 7]&lt;/code&gt; was rotated at the pivot, becoming &lt;code&gt;[4, 5, 6, 7, 0, 1, 2]&lt;/code&gt;. The target value &lt;code&gt;0&lt;/code&gt; is at index 4.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;In a rotated sorted array, at least one half (left or right of mid) is always properly sorted. You can check if the target falls within the sorted half&amp;rsquo;s range. If it does, search there; otherwise, search the other half. This preserves the O(log n) binary search guarantee.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Spiral Matrix</title>
      <link>https://chiraghasija.cc/dsa/spiral-matrix/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/spiral-matrix/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;Matrix:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Output: &lt;code&gt;[1, 2, 3, 6, 9, 8, 7, 4, 5]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Reading the matrix in spiral order means: go right along the top row (1, 2, 3), then down the right column (6, 9), then left along the bottom row (8, 7), then up the left column (4), and finally the center element (5).&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Instead of tracking visited cells, you maintain four boundaries that define the unvisited rectangle. Each spiral pass peels off one layer: right across the top, down the right side, left across the bottom, up the left side. After each direction, the corresponding boundary shrinks inward. The boundary checks prevent double-counting rows or columns in non-square matrices.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Subsets</title>
      <link>https://chiraghasija.cc/dsa/subsets/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/subsets/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;nums = [1, 2, 3]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;[[], [1], [1,2], [1,2,3], [1,3], [2], [2,3], [3]]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The power set contains all 2^3 = 8 possible subsets, from the empty set to the full set. Each element is either included or excluded, and no two subsets are the same.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Every subset is a sequence of include/exclude decisions for each element. By recording the current path at every level of recursion (not just at the leaves), you capture subsets of all sizes. The start index ensures each element only appears after the ones before it, preventing duplicates like {1,2} and {2,1}.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Top K Frequent Elements</title>
      <link>https://chiraghasija.cc/dsa/top-k-frequent-elements/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/top-k-frequent-elements/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;nums = [1, 1, 1, 2, 2, 3]&lt;/code&gt;, &lt;code&gt;k = 2&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;[1, 2]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The number &lt;code&gt;1&lt;/code&gt; appears 3 times and &lt;code&gt;2&lt;/code&gt; appears 2 times, making them the two most frequent elements. The number &lt;code&gt;3&lt;/code&gt; only appears once, so it is not included.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;You need the top k elements by frequency, but sorting costs O(n log n). Instead, use bucket sort: create an array where position i holds numbers that appear i times. Since no number can appear more than n times, you just walk backwards from the last bucket to grab the most frequent elements first.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Two Sum</title>
      <link>https://chiraghasija.cc/dsa/two-sum/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/two-sum/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;nums = [2, 7, 11, 15]&lt;/code&gt;, &lt;code&gt;target = 9&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;[0, 1]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Because &lt;code&gt;nums[0] + nums[1] = 2 + 7 = 9&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;For each number, you need to find if its &amp;ldquo;partner&amp;rdquo; (target minus current) exists. Instead of scanning the whole array each time (O(n^2)), store every number you&amp;rsquo;ve seen in a hashmap. This lets you check for the partner in O(1).&lt;/p&gt;
&lt;h2 id=&#34;step-by-step&#34;&gt;Step by step&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Walk through the array&lt;/strong&gt; — for each number, compute what value you&amp;rsquo;d need to reach the target.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Check the hashmap&lt;/strong&gt; — if that complement value has been seen before, you found your answer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Store and move on&lt;/strong&gt; — if not found, record the current number and its index for future lookups.&lt;/li&gt;
&lt;/ol&gt;</description>
    </item>
    <item>
      <title>Unique Paths</title>
      <link>https://chiraghasija.cc/dsa/unique-paths/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/unique-paths/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;m = 3&lt;/code&gt;, &lt;code&gt;n = 3&lt;/code&gt; (3x3 grid)&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;6&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The robot must move right exactly 2 times and down exactly 2 times to go from the top-left to the bottom-right. The 6 paths correspond to the 6 different orderings of these 4 moves (e.g., RRDD, RDRD, RDDR, DRRD, DRDR, DDRR).&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;You can only move right or down, so the number of ways to reach any cell equals the ways to reach the cell above it plus the ways to reach the cell to its left. The top row and left column each have exactly one path (all-right or all-down), which gives you the base cases to build from.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Valid Anagram</title>
      <link>https://chiraghasija.cc/dsa/valid-anagram/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/valid-anagram/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;s = &amp;quot;anagram&amp;quot;&lt;/code&gt;, &lt;code&gt;t = &amp;quot;nagaram&amp;quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Both strings contain exactly the same letters with the same frequencies: three &lt;code&gt;a&lt;/code&gt;s, one &lt;code&gt;n&lt;/code&gt;, one &lt;code&gt;g&lt;/code&gt;, one &lt;code&gt;r&lt;/code&gt;, and one &lt;code&gt;m&lt;/code&gt;. Since &lt;code&gt;t&lt;/code&gt; is just a rearrangement of &lt;code&gt;s&lt;/code&gt;, it is a valid anagram.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Two strings are anagrams if they use exactly the same letters the same number of times. By incrementing a counter for each character in &lt;code&gt;s&lt;/code&gt; and decrementing for each character in &lt;code&gt;t&lt;/code&gt;, you end up with all zeros only when the frequencies match perfectly.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Valid Palindrome</title>
      <link>https://chiraghasija.cc/dsa/valid-palindrome/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/valid-palindrome/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;s = &amp;quot;A man, a plan, a canal: Panama&amp;quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;After removing non-alphanumeric characters and converting to lowercase, the string becomes &lt;code&gt;&amp;quot;amanaplanacanalpanama&amp;quot;&lt;/code&gt;, which reads the same forwards and backwards.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;A palindrome reads the same forwards and backwards. Instead of creating a cleaned copy of the string, use two pointers from each end, skipping non-alphanumeric characters and comparing in lowercase. If every pair matches, it is a palindrome.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Valid Parentheses</title>
      <link>https://chiraghasija.cc/dsa/valid-parentheses/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/valid-parentheses/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;s = &amp;quot;{[()]}&amp;quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Every opening bracket has a matching closing bracket in the correct order: &lt;code&gt;(&lt;/code&gt; is closed by &lt;code&gt;)&lt;/code&gt;, &lt;code&gt;[&lt;/code&gt; is closed by &lt;code&gt;]&lt;/code&gt;, and &lt;code&gt;{&lt;/code&gt; is closed by &lt;code&gt;}&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Brackets must close in the reverse order they open &amp;ndash; the most recent opener must be closed first. A stack naturally enforces this &amp;ldquo;last-in, first-out&amp;rdquo; order. Every time you see a closing bracket, you check that the top of the stack is the correct matching opener.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Validate Binary Search Tree</title>
      <link>https://chiraghasija.cc/dsa/validate-binary-search-tree/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/validate-binary-search-tree/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;        5
       / \
      1   7
         / \
        4   8
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Output: &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Node 4 is in the right subtree of 5, but 4 &amp;lt; 5. In a valid BST, every node in the right subtree must be greater than the root. Even though 4 &amp;lt; 7 satisfies the local parent-child rule, it violates the global BST property.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;A BST is not just &amp;ldquo;left child &amp;lt; parent &amp;lt; right child&amp;rdquo; at each node — every node in the left subtree must be less than the root, and every node in the right subtree must be greater. By passing an allowed range &lt;code&gt;(low, high)&lt;/code&gt; down the recursion and tightening it at each step, you enforce this global constraint without needing to look back up the tree.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Word Break</title>
      <link>https://chiraghasija.cc/dsa/word-break/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://chiraghasija.cc/dsa/word-break/</guid>
      <description>&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;s = &amp;quot;leetcode&amp;quot;&lt;/code&gt;, &lt;code&gt;wordDict = [&amp;quot;leet&amp;quot;, &amp;quot;code&amp;quot;]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Output: &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The string can be segmented as &lt;code&gt;&amp;quot;leet&amp;quot;&lt;/code&gt; + &lt;code&gt;&amp;quot;code&amp;quot;&lt;/code&gt;, and both words are in the dictionary.&lt;/p&gt;
&lt;h2 id=&#34;why-this-works&#34;&gt;Why this works&lt;/h2&gt;
&lt;p&gt;Instead of trying every possible way to split the string (exponential), you build up answers left to right. If you know that the first &lt;code&gt;j&lt;/code&gt; characters can be split into valid words, and the substring from &lt;code&gt;j&lt;/code&gt; to &lt;code&gt;i&lt;/code&gt; is also a dictionary word, then the first &lt;code&gt;i&lt;/code&gt; characters can be split too. This avoids re-solving the same sub-problems.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
