<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>solve</title>
    <link>https://solve.bwang.io/</link>
    <description></description>
    <pubDate>Sun, 05 Apr 2026 01:07:24 +0000</pubDate>
    <item>
      <title>Binary tree maximum path sum</title>
      <link>https://solve.bwang.io/binary-tree-maximum-path-sum?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[problem&#xA;&#xA;This was the first problem I did that I needed a bit of help, but once I saw that maxpath state should be kept as a global variable, it made the coding much easier. I also needed the hint for max(left, 0); 0 implying that we don&#39;t take the left.&#xA;&#xA;Will need to review this problem.&#xA;&#xA;from typing import Optional&#xA;Definition for a binary tree node.&#xA;class TreeNode:&#xA;def init(self, val=0, left=None, right=None):&#xA;self.val = val&#xA;self.left = left&#xA;self.right = right&#xA;class Solution:&#xA;    def maxPathSum(self, root: Optional[TreeNode]) -  int:&#xA;        self.maxpath = -1001&#xA;        self.dfs(root)&#xA;        return self.maxpath&#xA;    &#xA;    # returns maxEdge&#xA;    def dfs(self, node) -  int:&#xA;&#xA;        if not node:&#xA;            return 0&#xA;&#xA;        left = max(self.dfs(node.left), 0)&#xA;        right = max(self.dfs(node.right), 0)&#xA;        self.maxpath = max(self.max_path, left + right + node.val)&#xA;        &#xA;        return max(left + node.val, right + node.val)&#xA;&#xA;`]]&gt;</description>
      <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/binary-tree-maximum-path-sum/">problem</a></p>

<p>This was the first problem I did that I needed a bit of help, but once I saw that max_path state should be kept as a global variable, it made the coding much easier. I also needed the hint for max(left, 0); 0 implying that we don&#39;t take the left.</p>

<p>Will need to review this problem.</p>

<pre><code class="language-python">from typing import Optional
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxPathSum(self, root: Optional[TreeNode]) -&gt; int:
        self.max_path = -1001
        self.dfs(root)
        return self.max_path
    
    # returns maxEdge
    def dfs(self, node) -&gt; int:

        if not node:
            return 0

        left = max(self.dfs(node.left), 0)
        right = max(self.dfs(node.right), 0)
        self.max_path = max(self.max_path, left + right + node.val)
        
        return max(left + node.val, right + node.val)

</code></pre>
]]></content:encoded>
      <guid>https://solve.bwang.io/binary-tree-maximum-path-sum</guid>
      <pubDate>Sat, 20 Jan 2024 08:07:48 +0000</pubDate>
    </item>
    <item>
      <title>Sum root to leaf</title>
      <link>https://solve.bwang.io/sum-root-to-leaf?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[problem&#xA;&#xA;Same as the last problem. We only want to add when there&#39;s no left or right node. State is kept by function.&#xA;&#xA;from typing import Optional&#xA;Definition for a binary tree node.&#xA;class TreeNode:&#xA;def init(self, val=0, left=None, right=None):&#xA;self.val = val&#xA;self.left = left&#xA;self.right = right&#xA;class Solution:&#xA;    def sumNumbers(self, root: Optional[TreeNode]) -  int:&#xA;        return self.dfs(root, 0)&#xA;    &#xA;    def dfs(self, node, currSum) -  int:&#xA;&#xA;        if node == None:&#xA;            return 0&#xA;        &#xA;        if node.left == None and node.right == None:&#xA;            return int(str(currSum) + str(node.val))&#xA;        &#xA;        else:&#xA;            currSum = str(currSum) + str(node.val)&#xA;            return self.dfs(node.left, currSum) + self.dfs(node.right, currSum)&#xA;`]]&gt;</description>
      <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/sum-root-to-leaf-numbers/">problem</a></p>

<p>Same as the last problem. We only want to add when there&#39;s no left or right node. State is kept by function.</p>

<pre><code class="language-python">from typing import Optional
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def sumNumbers(self, root: Optional[TreeNode]) -&gt; int:
        return self.dfs(root, 0)
    
    def dfs(self, node, currSum) -&gt; int:

        if node == None:
            return 0
        
        if node.left == None and node.right == None:
            return int(str(currSum) + str(node.val))
        
        else:
            currSum = str(currSum) + str(node.val)
            return self.dfs(node.left, currSum) + self.dfs(node.right, currSum)
</code></pre>
]]></content:encoded>
      <guid>https://solve.bwang.io/sum-root-to-leaf</guid>
      <pubDate>Sat, 20 Jan 2024 00:24:59 +0000</pubDate>
    </item>
    <item>
      <title>Path sum</title>
      <link>https://solve.bwang.io/path-sum?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[Problem&#xA;&#xA;DFS, simple question: only check when we are at a leaf node (somehow my solution was top 99% in terms of both time and space complexity) &#xA;&#xA;from typing import Optional&#xA;Definition for a binary tree node.&#xA;class TreeNode:&#xA;def init(self, val=0, left=None, right=None):&#xA;self.val = val&#xA;self.left = left&#xA;self.right = right&#xA;class Solution:&#xA;    def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -  bool:&#xA;        return self.dfs(root, currSum=0, targetSum=targetSum)&#xA;&#xA;    def dfs(self, root, currSum, targetSum) -  bool:&#xA;&#xA;        if not root:&#xA;            return False&#xA;        &#xA;        if not root.left and not root.right:&#xA;            if root.val + currSum == targetSum:&#xA;                return True&#xA;&#xA;        else:&#xA;            currSum = root.val + currSum&#xA;            return self.dfs(root.left, currSum, targetSum) or self.dfs(root.right, currSum, targetSum)&#xA;&#xA;`]]&gt;</description>
      <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/path-sum">Problem</a></p>

<p>DFS, simple question: only check when we are at a leaf node (somehow my solution was top 99% in terms of both time and space complexity)</p>

<pre><code class="language-python">from typing import Optional
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -&gt; bool:
        return self.dfs(root, currSum=0, targetSum=targetSum)

    def dfs(self, root, currSum, targetSum) -&gt; bool:

        if not root:
            return False
        
        if not root.left and not root.right:
            if root.val + currSum == targetSum:
                return True

        else:
            currSum = root.val + currSum
            return self.dfs(root.left, currSum, targetSum) or self.dfs(root.right, currSum, targetSum)

</code></pre>
]]></content:encoded>
      <guid>https://solve.bwang.io/path-sum</guid>
      <pubDate>Fri, 19 Jan 2024 00:12:30 +0000</pubDate>
    </item>
    <item>
      <title>Flatten binary tree</title>
      <link>https://solve.bwang.io/flatten-binary-tree?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[problem&#xA;&#xA;I understand the solution but it&#39;s a bit difficult for me to come up on the spot. Need review.&#xA;&#xA;from typing import Optional&#xA;Definition for a binary tree node.&#xA;class TreeNode:&#xA;def init(self, val=0, left=None, right=None):&#xA;self.val = val&#xA;self.left = left&#xA;self.right = right&#xA;class Solution:&#xA;    def flatten(self, root: Optional[TreeNode]) -  None:&#xA;        self.dfs(root)&#xA;    &#xA;    def dfs(self, root):&#xA;        if not root:&#xA;            return None&#xA;        &#xA;        left = self.dfs(root.left)&#xA;        right = self.dfs(root.right)&#xA;&#xA;        if left:&#xA;            left.right = root.right&#xA;            root.right = root.left&#xA;            root.left = None&#xA;        &#xA;        if right:&#xA;            return right&#xA;        elif left:&#xA;            return left&#xA;        return root&#xA;`]]&gt;</description>
      <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/flatten-binary-tree-to-linked-list/">problem</a></p>

<p>I understand the solution but it&#39;s a bit difficult for me to come up on the spot. Need review.</p>

<pre><code class="language-python">from typing import Optional
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def flatten(self, root: Optional[TreeNode]) -&gt; None:
        self.dfs(root)
    
    def dfs(self, root):
        if not root:
            return None
        
        left = self.dfs(root.left)
        right = self.dfs(root.right)

        if left:
            left.right = root.right
            root.right = root.left
            root.left = None
        
        if right:
            return right
        elif left:
            return left
        return root
</code></pre>
]]></content:encoded>
      <guid>https://solve.bwang.io/flatten-binary-tree</guid>
      <pubDate>Wed, 17 Jan 2024 22:06:20 +0000</pubDate>
    </item>
    <item>
      <title>Next right pointer</title>
      <link>https://solve.bwang.io/next-right-pointer?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[Still taking it easy. It&#39;s a binary tree week and just taking it easy because I&#39;m tired.&#xA;&#xA;problem&#xA;&#xA;&#34;&#34;&#34;&#xA;Definition for a Node.&#xA;class Node:&#xA;    def init(self, val: int = 0, left: &#39;Node&#39; = None, right: &#39;Node&#39; = None, next: &#39;Node&#39; = None):&#xA;        self.val = val&#xA;        self.left = left&#xA;        self.right = right&#xA;        self.next = next&#xA;&#34;&#34;&#34;&#xA;&#xA;class Solution:&#xA;    def connect(self, root):&#xA;        self.processrow([root])&#xA;        return root&#xA;    &#xA;    def processrow(self, rowlist):&#xA;        nextrow = []&#xA;        for n in rowlist:&#xA;            if n == None:&#xA;                continue&#xA;            if n.left:&#xA;                nextrow.append(n.left)&#xA;            if n.right:&#xA;                nextrow.append(n.right)&#xA;        &#xA;        if not nextrow:&#xA;            return&#xA;&#xA;        for i, n in enumerate(nextrow[:len(nextrow)-1]):&#xA;            n.next = nextrow[i+1]&#xA;&#xA;        self.processrow(next_row)&#xA;`]]&gt;</description>
      <content:encoded><![CDATA[<p>Still taking it easy. It&#39;s a binary tree week and just taking it easy because I&#39;m tired.</p>

<p><a href="https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/">problem</a></p>

<pre><code class="language-python">&#34;&#34;&#34;
# Definition for a Node.
class Node:
    def __init__(self, val: int = 0, left: &#39;Node&#39; = None, right: &#39;Node&#39; = None, next: &#39;Node&#39; = None):
        self.val = val
        self.left = left
        self.right = right
        self.next = next
&#34;&#34;&#34;

class Solution:
    def connect(self, root):
        self.process_row([root])
        return root
    
    def process_row(self, row_list):
        next_row = []
        for n in row_list:
            if n == None:
                continue
            if n.left:
                next_row.append(n.left)
            if n.right:
                next_row.append(n.right)
        
        if not next_row:
            return

        for i, n in enumerate(next_row[:len(next_row)-1]):
            n.next = next_row[i+1]

        self.process_row(next_row)
</code></pre>
]]></content:encoded>
      <guid>https://solve.bwang.io/next-right-pointer</guid>
      <pubDate>Wed, 17 Jan 2024 06:55:21 +0000</pubDate>
    </item>
    <item>
      <title>Binary tree from order</title>
      <link>https://solve.bwang.io/construct-binary-tree-from-preorder-and-inorder?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[It&#39;s MLK day, here&#39;s some more binary tree questions&#xA;&#xA;preorder and inorder:&#xA;problem&#xA;&#xA;from typing import Optional&#xA;Definition for a binary tree node.&#xA;class TreeNode:&#xA;def init(self, val=0, left=None, right=None):&#xA;self.val = val&#xA;self.left = left&#xA;self.right = right&#xA;class Solution:&#xA;    def buildTree(self, preorder: List[int], inorder: List[int]) -  Optional[TreeNode]:&#xA;&#xA;        if not preorder:&#xA;            return None&#xA;&#xA;        val = preorder[0]&#xA;&#xA;        splitindex = -1&#xA;        for i, v in enumerate(inorder):&#xA;            if v == val:&#xA;                splitindex = i&#xA;&#xA;        leftpreorder = preorder[1:1+splitindex]&#xA;        rightpreorder = preorder[1+splitindex:]&#xA;        leftinorder = inorder[0:splitindex]&#xA;        rightinorder = inorder[splitindex+1:]&#xA;&#xA;        return TreeNode(&#xA;            val = val,&#xA;            left = self.buildTree(leftpreorder, leftinorder),&#xA;            right = self.buildTree(rightpreorder, rightinorder)&#xA;        )&#xA;&#xA;inorder and postorder:&#xA;problem&#xA;&#xA;from typing import Optional&#xA;Definition for a binary tree node.&#xA;class TreeNode:&#xA;def init(self, val=0, left=None, right=None):&#xA;self.val = val&#xA;self.left = left&#xA;self.right = right&#xA;class Solution:&#xA;    def buildTree(self, inorder: List[int], postorder: List[int]) -  Optional[TreeNode]:&#xA;        if not inorder:&#xA;            return None&#xA;        val = postorder[-1]&#xA;        splitindex = -1&#xA;        for i, v in enumerate(inorder):&#xA;            if v == val:&#xA;                splitindex = i&#xA;        leftinorder = inorder[0:splitindex]&#xA;        rightinorder = inorder[splitindex+1:]&#xA;        leftpostorder = postorder[0:splitindex]&#xA;        rightpostorder = postorder[splitindex:len(postorder)-1]&#xA;&#xA;        return TreeNode(&#xA;            val=val,&#xA;            left=self.buildTree(leftinorder, leftpostorder),&#xA;            right=self.buildTree(rightinorder, rightpostorder)&#xA;        )&#xA;&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p>It&#39;s MLK day, here&#39;s some more binary tree questions</p>

<p>preorder and inorder:
<a href="https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/">problem</a></p>

<pre><code class="language-python">from typing import Optional
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def buildTree(self, preorder: List[int], inorder: List[int]) -&gt; Optional[TreeNode]:

        if not preorder:
            return None

        val = preorder[0]

        split_index = -1
        for i, v in enumerate(inorder):
            if v == val:
                split_index = i

        left_preorder = preorder[1:1+split_index]
        right_preorder = preorder[1+split_index:]
        left_inorder = inorder[0:split_index]
        right_inorder = inorder[split_index+1:]

        return TreeNode(
            val = val,
            left = self.buildTree(left_preorder, left_inorder),
            right = self.buildTree(right_preorder, right_inorder)
        )
</code></pre>

<p>inorder and postorder:
<a href="https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/">problem</a></p>

<pre><code class="language-python">from typing import Optional
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def buildTree(self, inorder: List[int], postorder: List[int]) -&gt; Optional[TreeNode]:
        if not inorder:
            return None
        val = postorder[-1]
        split_index = -1
        for i, v in enumerate(inorder):
            if v == val:
                split_index = i
        left_inorder = inorder[0:split_index]
        right_inorder = inorder[split_index+1:]
        left_postorder = postorder[0:split_index]
        right_postorder = postorder[split_index:len(postorder)-1]

        return TreeNode(
            val=val,
            left=self.buildTree(left_inorder, left_postorder),
            right=self.buildTree(right_inorder, right_postorder)
        )
</code></pre>
]]></content:encoded>
      <guid>https://solve.bwang.io/construct-binary-tree-from-preorder-and-inorder</guid>
      <pubDate>Tue, 16 Jan 2024 06:53:18 +0000</pubDate>
    </item>
    <item>
      <title>Symmetric tree</title>
      <link>https://solve.bwang.io/symmetric-tree?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[It&#39;s still the weekend so taking it easy. Some binary tree questions.&#xA;&#xA;problem&#xA;&#xA;from typing import Optional&#xA;Definition for a binary tree node.&#xA;class TreeNode:&#xA;def init(self, val=0, left=None, right=None):&#xA;self.val = val&#xA;self.left = left&#xA;self.right = right&#xA;class Solution:&#xA;    def isSymmetric(self, root: Optional[TreeNode]) -  bool:&#xA;        return self.ismirror(root.left, root.right)&#xA;    &#xA;    def ismirror(self, p, q):&#xA;        if p is None and q is None:&#xA;            return True&#xA;        elif p is not None and q is None:&#xA;            return False&#xA;        elif p is None and q is not None:&#xA;            return False&#xA;        # elif p is not None and q is not None:&#xA;        if p.val != q.val:&#xA;            return False&#xA;        &#xA;        return self.ismirror(p.right, q.left) and self.ismirror(p.left, q.right)&#xA;&#xA;problem&#xA;&#xA;from typing import Optional&#xA;Definition for a binary tree node.&#xA;class TreeNode:&#xA;def init(self, val=0, left=None, right=None):&#xA;self.val = val&#xA;self.left = left&#xA;self.right = right&#xA;class Solution:&#xA;    def invertTree(self, root: Optional[TreeNode]) -  Optional[TreeNode]:&#xA;        if root == None:&#xA;            return None&#xA;        placeholder = root.left&#xA;        root.left = self.invertTree(root.right)&#xA;        root.right = self.invertTree(placeholder)&#xA;        return root&#xA;`]]&gt;</description>
      <content:encoded><![CDATA[<p>It&#39;s still the weekend so taking it easy. Some binary tree questions.</p>

<p><a href="https://leetcode.com/problems/symmetric-tree/">problem</a></p>

<pre><code class="language-python">from typing import Optional
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSymmetric(self, root: Optional[TreeNode]) -&gt; bool:
        return self.is_mirror(root.left, root.right)
    
    def is_mirror(self, p, q):
        if p is None and q is None:
            return True
        elif p is not None and q is None:
            return False
        elif p is None and q is not None:
            return False
        # elif p is not None and q is not None:
        if p.val != q.val:
            return False
        
        return self.is_mirror(p.right, q.left) and self.is_mirror(p.left, q.right)
</code></pre>

<p><a href="https://leetcode.com/problems/invert-binary-tree">problem</a></p>

<pre><code class="language-python">from typing import Optional
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def invertTree(self, root: Optional[TreeNode]) -&gt; Optional[TreeNode]:
        if root == None:
            return None
        placeholder = root.left
        root.left = self.invertTree(root.right)
        root.right = self.invertTree(placeholder)
        return root
</code></pre>
]]></content:encoded>
      <guid>https://solve.bwang.io/symmetric-tree</guid>
      <pubDate>Mon, 15 Jan 2024 06:49:55 +0000</pubDate>
    </item>
    <item>
      <title>Same tree</title>
      <link>https://solve.bwang.io/same-tree?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[The next few problems these days are all going to be binary tree problems. Starting off easy because it&#39;s the weekend&#xA;&#xA;problems&#xA;&#xA;from typing import Optional&#xA;Definition for a binary tree node.&#xA;class TreeNode:&#xA;def init(self, val=0, left=None, right=None):&#xA;self.val = val&#xA;self.left = left&#xA;self.right = right&#xA;class Solution:&#xA;    def maxDepth(self, root: Optional[TreeNode]) -  int:&#xA;        if root == None:&#xA;            return 0&#xA;&#xA;        return max(1 + self.maxDepth(root.left), 1 + self.maxDepth(root.right))&#xA;&#xA;problem&#xA;&#xA;from typing import Optional&#xA;Definition for a binary tree node.&#xA;class TreeNode:&#xA;def init(self, val=0, left=None, right=None):&#xA;self.val = val&#xA;self.left = left&#xA;self.right = right&#xA;class Solution:&#xA;    def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -  bool:&#xA;        if not p and not q:&#xA;            return True&#xA;        if not p and q:&#xA;            return False&#xA;        if p and not q:&#xA;            return False&#xA;        if p and q:&#xA;            if p.val != q.val:&#xA;                return False&#xA;        &#xA;        return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)&#xA;&#xA;`]]&gt;</description>
      <content:encoded><![CDATA[<p>The next few problems these days are all going to be binary tree problems. Starting off easy because it&#39;s the weekend</p>

<p><a href="https://leetcode.com/problems/maximum-depth-of-binary-tree/">problems</a></p>

<pre><code class="language-python">from typing import Optional
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -&gt; int:
        if root == None:
            return 0

        return max(1 + self.maxDepth(root.left), 1 + self.maxDepth(root.right))
</code></pre>

<p><a href="https://leetcode.com/problems/same-tree/description/">problem</a></p>

<pre><code class="language-python">from typing import Optional
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -&gt; bool:
        if not p and not q:
            return True
        if not p and q:
            return False
        if p and not q:
            return False
        if p and q:
            if p.val != q.val:
                return False
        
        return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)


</code></pre>
]]></content:encoded>
      <guid>https://solve.bwang.io/same-tree</guid>
      <pubDate>Sun, 14 Jan 2024 00:00:21 +0000</pubDate>
    </item>
    <item>
      <title>Minimum window substring</title>
      <link>https://solve.bwang.io/minimum-window-substring?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[problem&#xA;&#xA;This week is just sliding window questions. Not a hard problem to conceptualize: two pointers and a hash table to keep state. However, took me a while to code it out / was stuck on an edge case.&#xA;&#xA;from collections import Counter&#xA;&#xA;class Solution:&#xA;    def minWindow(self, s: str, t: str) -  str:&#xA;        targetdict = Counter(t)&#xA;        targetsum = sum(targetdict.values())&#xA;        currsum = 0&#xA;        currdict = {}&#xA;        p1 = 0&#xA;        p2 = -1&#xA;&#xA;        currminsubstringlength = len(s) + 1&#xA;        currminsubstring = s&#xA;&#xA;        while True:&#xA;&#xA;            # we want to move p2 because we haven&#39;t hit our targetdict yet&#xA;            if currsum &lt; targetsum:&#xA;                p2 = p2 + 1&#xA;&#xA;                if p2   = len(s):&#xA;                    break&#xA;&#xA;                lastchar = s[p2]&#xA;                if targetdict.get(lastchar):&#xA;                    if currdict.get(lastchar):&#xA;                        currdict[lastchar] = currdict[lastchar] + 1&#xA;                    else:&#xA;                        currdict[lastchar] = 1&#xA;                &#xA;                    if targetdict[lastchar]   = currdict[lastchar]:&#xA;                        currsum = currsum + 1&#xA;            &#xA;            # we want to move p1 because we have hit our target&#xA;            else: # currsum == targetsum&#xA;&#xA;                if p2 - p1 + 1 &lt; currminsubstringlength:&#xA;                    currminsubstring = s[p1:p2+1]&#xA;                    currminsubstringlength = p2 - p1 + 1&#xA;&#xA;                firstchar = s[p1]&#xA;                if targetdict.get(firstchar):&#xA;                    currdict[firstchar] = currdict[firstchar] - 1&#xA;                    &#xA;                    if targetdict[firstchar]   currdict[firstchar]:&#xA;                        currsum = currsum - 1&#xA;                &#xA;                p1 = p1 + 1&#xA;                &#xA;                if p1   p2:&#xA;                    p1 = p1 - 1&#xA;                    continue&#xA;&#xA;                while targetdict.get(s[p1]) == None:&#xA;                    if currsum == targetsum:&#xA;                        if p2 - p1 + 1 &lt; currminsubstringlength:&#xA;                            currminsubstring = s[p1:p2+1]&#xA;                            currminsubstringlength = p2 - p1 + 1&#xA;                    p1 = p1 + 1&#xA;&#xA;        if currminsubstringlength == len(s) + 1:&#xA;            return &#34;&#34;&#xA;        &#xA;        return currmin_substring&#xA;`]]&gt;</description>
      <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/minimum-window-substring">problem</a></p>

<p>This week is just sliding window questions. Not a hard problem to conceptualize: two pointers and a hash table to keep state. However, took me a while to code it out / was stuck on an edge case.</p>

<pre><code class="language-python">from collections import Counter

class Solution:
    def minWindow(self, s: str, t: str) -&gt; str:
        target_dict = Counter(t)
        target_sum = sum(target_dict.values())
        curr_sum = 0
        curr_dict = {}
        p1 = 0
        p2 = -1

        curr_min_substring_length = len(s) + 1
        curr_min_substring = s

        while True:

            # we want to move p2 because we haven&#39;t hit our target_dict yet
            if curr_sum &lt; target_sum:
                p2 = p2 + 1

                if p2 &gt;= len(s):
                    break

                last_char = s[p2]
                if target_dict.get(last_char):
                    if curr_dict.get(last_char):
                        curr_dict[last_char] = curr_dict[last_char] + 1
                    else:
                        curr_dict[last_char] = 1
                
                    if target_dict[last_char] &gt;= curr_dict[last_char]:
                        curr_sum = curr_sum + 1
            
            # we want to move p1 because we have hit our target
            else: # curr_sum == target_sum

                if p2 - p1 + 1 &lt; curr_min_substring_length:
                    curr_min_substring = s[p1:p2+1]
                    curr_min_substring_length = p2 - p1 + 1

                first_char = s[p1]
                if target_dict.get(first_char):
                    curr_dict[first_char] = curr_dict[first_char] - 1
                    
                    if target_dict[first_char] &gt; curr_dict[first_char]:
                        curr_sum = curr_sum - 1
                
                p1 = p1 + 1
                
                if p1 &gt; p2:
                    p1 = p1 - 1
                    continue

                while target_dict.get(s[p1]) == None:
                    if curr_sum == target_sum:
                        if p2 - p1 + 1 &lt; curr_min_substring_length:
                            curr_min_substring = s[p1:p2+1]
                            curr_min_substring_length = p2 - p1 + 1
                    p1 = p1 + 1

        if curr_min_substring_length == len(s) + 1:
            return &#34;&#34;
        
        return curr_min_substring
</code></pre>
]]></content:encoded>
      <guid>https://solve.bwang.io/minimum-window-substring</guid>
      <pubDate>Fri, 12 Jan 2024 08:18:28 +0000</pubDate>
    </item>
    <item>
      <title>Substring concat of words</title>
      <link>https://solve.bwang.io/substring-concat-of-words?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[problem&#xA;&#xA;This week is just sliding window questions.&#xA;&#xA;Confusing wording. Can be optimized to actually use &#34;pointers&#34; and actually be a sliding window problem, but that requires more work and I&#39;m lazy (go through the array len(words[0)] time and in each time, removing the last one and adding the next one, etc.). Good enough...&#xA;&#xA;from collections import Counter&#xA;&#xA;class Solution:&#xA;    def findSubstring(self, s: str, words: List[str]) -  List[int]:&#xA;        &#xA;        targetmap = Counter(words)&#xA;        singlewordlength = len(words[0])&#xA;        wordslength = len(words)&#xA;        concattotallength = wordslength  singlewordlength&#xA;        solution = []&#xA;&#xA;        if concattotallength   len(s):&#xA;            return solution&#xA;&#xA;        for k in range(0, singlewordlength):&#xA;            for i in range(0, len(s)):&#xA;&#xA;                concatword = s[i:i+concattotallength]&#xA;                currmap = {}&#xA;&#xA;                for j in range(0, wordslength):&#xA;                    word = concatword[jsinglewordlength : (j+1) * singlewordlength]&#xA;                    if currmap.get(word):&#xA;                        currmap[word] = currmap[word] + 1&#xA;                    else:&#xA;                        currmap[word] = 1&#xA;            &#xA;                if currmap == target_map:&#xA;                    solution.append(i)&#xA;        &#xA;        return solution&#xA;`]]&gt;</description>
      <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/substring-with-concatenation-of-all-words/">problem</a></p>

<p>This week is just sliding window questions.</p>

<p>Confusing wording. Can be optimized to actually use “pointers” and actually be a sliding window problem, but that requires more work and I&#39;m lazy (go through the array <code>len(words[0)]</code> time and in each time, removing the last one and adding the next one, etc.). Good enough...</p>

<pre><code class="language-python">from collections import Counter

class Solution:
    def findSubstring(self, s: str, words: List[str]) -&gt; List[int]:
        
        target_map = Counter(words)
        single_word_length = len(words[0])
        words_length = len(words)
        concat_total_length = words_length * single_word_length
        solution = []

        if concat_total_length &gt; len(s):
            return solution

        for k in range(0, single_word_length):
            for i in range(0, len(s)):

                concat_word = s[i:i+concat_total_length]
                curr_map = {}

                for j in range(0, words_length):
                    word = concat_word[j*single_word_length : (j+1) * single_word_length]
                    if curr_map.get(word):
                        curr_map[word] = curr_map[word] + 1
                    else:
                        curr_map[word] = 1
            
                if curr_map == target_map:
                    solution.append(i)
        
        return solution
</code></pre>
]]></content:encoded>
      <guid>https://solve.bwang.io/substring-concat-of-words</guid>
      <pubDate>Thu, 11 Jan 2024 23:18:08 +0000</pubDate>
    </item>
  </channel>
</rss>