<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Abhishek Chaudhary</title>
    <description>The latest articles on Forem by Abhishek Chaudhary (@theabbie).</description>
    <link>https://forem.com/theabbie</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F315248%2F9ef2ec2a-378e-421a-910f-c75f4596d178.jpg</url>
      <title>Forem: Abhishek Chaudhary</title>
      <link>https://forem.com/theabbie</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/theabbie"/>
    <language>en</language>
    <item>
      <title>Canvas API</title>
      <dc:creator>Abhishek Chaudhary</dc:creator>
      <pubDate>Thu, 21 Mar 2024 03:09:14 +0000</pubDate>
      <link>https://forem.com/theabbie/canvas-api-1db7</link>
      <guid>https://forem.com/theabbie/canvas-api-1db7</guid>
      <description>&lt;p&gt;&lt;em&gt;This is a submission for DEV Challenge v24.03.20, One Byte Explainer: Browser API or Feature.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Explainer
&lt;/h2&gt;

&lt;p&gt;The Canvas API provides a drawing surface for creating visually engaging graphics and images using JavaScript to dynamically render shapes, paths, text, and images, enabling the creation of interactive charts, animations, and games on the web.&lt;/p&gt;

</description>
      <category>frontendchallenge</category>
      <category>devchallenge</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Serialize and Deserialize BST</title>
      <dc:creator>Abhishek Chaudhary</dc:creator>
      <pubDate>Sun, 19 Jun 2022 04:00:48 +0000</pubDate>
      <link>https://forem.com/theabbie/serialize-and-deserialize-bst-45dm</link>
      <guid>https://forem.com/theabbie/serialize-and-deserialize-bst-45dm</guid>
      <description>&lt;p&gt;Serialization is converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.&lt;/p&gt;

&lt;p&gt;Design an algorithm to serialize and deserialize a &lt;strong&gt;binary search tree&lt;/strong&gt;. There is no restriction on how your serialization/deserialization algorithm should work. You need to ensure that a binary search tree can be serialized to a string, and this string can be deserialized to the original tree structure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The encoded string should be as compact as possible.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; root = [2,1,3]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [2,1,3]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; root = []&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; []&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The number of nodes in the tree is in the range &lt;code&gt;[0, 104]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;0 &amp;lt;= Node.val &amp;lt;= 104&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  The input tree is &lt;strong&gt;guaranteed&lt;/strong&gt; to be a binary search tree.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;SOLUTION:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Codec:

    def serialize(self, root: Optional[TreeNode]) -&amp;gt; str:
        tree = {}
        paths = [(root, 0)]
        while len(paths) &amp;gt; 0:
            curr, i = paths.pop()
            if curr:
                tree[i] = curr.val
                paths.append((curr.left, 2 * i + 1))
                paths.append((curr.right, 2 * i + 2))
        return ";".join(["{}={}".format(k, v) for k, v in tree.items()])

    def deserialize(self, data: str) -&amp;gt; Optional[TreeNode]:
        vals = data.split(";")
        tree = {}
        for v in vals:
            if len(v) &amp;gt; 0:
                key, value = v.split("=")
                tree[int(key)] = int(value)
        if len(tree) == 0:
            return None
        root = TreeNode()
        paths = [(root, 0)]
        while len(paths) &amp;gt; 0:
            curr, i = paths.pop()
            curr.val = tree[i]
            if (2 * i + 1) in tree:
                curr.left = TreeNode()
                paths.append((curr.left, 2 * i + 1))
            if (2 * i + 2) in tree:
                curr.right = TreeNode()
                paths.append((curr.right, 2 * i + 2))
        return root

# Your Codec object will be instantiated and called as such:
# Your Codec object will be instantiated and called as such:
# ser = Codec()
# deser = Codec()
# tree = ser.serialize(root)
# ans = deser.deserialize(tree)
# return ans
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>theabbie</category>
    </item>
    <item>
      <title>Valid Palindrome</title>
      <dc:creator>Abhishek Chaudhary</dc:creator>
      <pubDate>Sun, 19 Jun 2022 03:30:36 +0000</pubDate>
      <link>https://forem.com/theabbie/valid-palindrome-5aoa</link>
      <guid>https://forem.com/theabbie/valid-palindrome-5aoa</guid>
      <description>&lt;p&gt;A phrase is a &lt;strong&gt;palindrome&lt;/strong&gt; if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.&lt;/p&gt;

&lt;p&gt;Given a string &lt;code&gt;s&lt;/code&gt;, return &lt;code&gt;true&lt;/code&gt; &lt;em&gt;if it is a &lt;strong&gt;palindrome&lt;/strong&gt;, or&lt;/em&gt; &lt;code&gt;false&lt;/code&gt; &lt;em&gt;otherwise&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; s = "A man, a plan, a canal: Panama"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; "amanaplanacanalpanama" is a palindrome.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; s = "race a car"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; "raceacar" is not a palindrome.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; s = " "&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; s is an empty string "" after removing non-alphanumeric characters.&lt;br&gt;
Since an empty string reads the same forward and backward, it is a palindrome.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;1 &amp;lt;= s.length &amp;lt;= 2 * 105&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;s&lt;/code&gt; consists only of printable ASCII characters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;SOLUTION:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def isPalindrome(self, s: str) -&amp;gt; bool:
        s = "".join([c.lower() for c in s if c.isalnum()])
        return s == s[::-1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>theabbie</category>
    </item>
    <item>
      <title>Average Salary Excluding the Minimum and Maximum Salary</title>
      <dc:creator>Abhishek Chaudhary</dc:creator>
      <pubDate>Sun, 19 Jun 2022 03:00:41 +0000</pubDate>
      <link>https://forem.com/theabbie/average-salary-excluding-the-minimum-and-maximum-salary-4gn6</link>
      <guid>https://forem.com/theabbie/average-salary-excluding-the-minimum-and-maximum-salary-4gn6</guid>
      <description>&lt;p&gt;You are given an array of &lt;strong&gt;unique&lt;/strong&gt; integers &lt;code&gt;salary&lt;/code&gt; where &lt;code&gt;salary[i]&lt;/code&gt; is the salary of the &lt;code&gt;ith&lt;/code&gt; employee.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the average salary of employees excluding the minimum and maximum salary&lt;/em&gt;. Answers within &lt;code&gt;10-5&lt;/code&gt; of the actual answer will be accepted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; salary = [4000,3000,1000,2000]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2500.00000&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Minimum salary and maximum salary are 1000 and 4000 respectively.&lt;br&gt;
Average salary excluding minimum and maximum salary is (2000+3000) / 2 = 2500&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; salary = [1000,2000,3000]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2000.00000&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Minimum salary and maximum salary are 1000 and 3000 respectively.&lt;br&gt;
Average salary excluding minimum and maximum salary is (2000) / 1 = 2000&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;3 &amp;lt;= salary.length &amp;lt;= 100&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;1000 &amp;lt;= salary[i] &amp;lt;= 106&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  All the integers of &lt;code&gt;salary&lt;/code&gt; are &lt;strong&gt;unique&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;SOLUTION:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def average(self, salary: List[int]) -&amp;gt; float:
        n = len(salary)
        total = 0
        currmin = float('inf')
        currmax = float('-inf')
        for i in range(n):
            currmin = min(currmin, salary[i])
            currmax = max(currmax, salary[i])
            total += salary[i]
        return (total - currmin - currmax) / (n - 2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>theabbie</category>
    </item>
    <item>
      <title>Backspace String Compare</title>
      <dc:creator>Abhishek Chaudhary</dc:creator>
      <pubDate>Sun, 19 Jun 2022 02:30:40 +0000</pubDate>
      <link>https://forem.com/theabbie/backspace-string-compare-5c0c</link>
      <guid>https://forem.com/theabbie/backspace-string-compare-5c0c</guid>
      <description>&lt;p&gt;Given two strings &lt;code&gt;s&lt;/code&gt; and &lt;code&gt;t&lt;/code&gt;, return &lt;code&gt;true&lt;/code&gt; &lt;em&gt;if they are equal when both are typed into empty text editors&lt;/em&gt;. &lt;code&gt;'#'&lt;/code&gt; means a backspace character.&lt;/p&gt;

&lt;p&gt;Note that after backspacing an empty text, the text will continue empty.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; s = "ab#c", t = "ad#c"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Both s and t become "ac".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; s = "ab##", t = "c#d#"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Both s and t become "".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; s = "a#c", t = "b"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; s becomes "c" while t becomes "b".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;1 &amp;lt;= s.length, t.length &amp;lt;= 200&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;s&lt;/code&gt; and &lt;code&gt;t&lt;/code&gt; only contain lowercase letters and &lt;code&gt;'#'&lt;/code&gt; characters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Follow up:&lt;/strong&gt; Can you solve it in &lt;code&gt;O(n)&lt;/code&gt; time and &lt;code&gt;O(1)&lt;/code&gt; space?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SOLUTION:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def backspaceCompare(self, s: str, t: str) -&amp;gt; bool:
        ss = ""
        tt = ""
        for c in s:
            if c == "#":
                ss = ss[:-1]
            else:
                ss += c
        for c in t:
            if c == "#":
                tt = tt[:-1]
            else:
                tt += c
        return ss == tt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>theabbie</category>
    </item>
    <item>
      <title>Maximum Erasure Value</title>
      <dc:creator>Abhishek Chaudhary</dc:creator>
      <pubDate>Sun, 19 Jun 2022 02:00:46 +0000</pubDate>
      <link>https://forem.com/theabbie/maximum-erasure-value-4m96</link>
      <guid>https://forem.com/theabbie/maximum-erasure-value-4m96</guid>
      <description>&lt;p&gt;You are given an array of positive integers &lt;code&gt;nums&lt;/code&gt; and want to erase a subarray containing &lt;strong&gt;unique elements&lt;/strong&gt;. The &lt;strong&gt;score&lt;/strong&gt; you get by erasing the subarray is equal to the &lt;strong&gt;sum&lt;/strong&gt; of its elements.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the &lt;strong&gt;maximum score&lt;/strong&gt; you can get by erasing &lt;strong&gt;exactly one&lt;/strong&gt; subarray.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;An array &lt;code&gt;b&lt;/code&gt; is called to be a subarray of &lt;code&gt;a&lt;/code&gt; if it forms a contiguous subsequence of &lt;code&gt;a&lt;/code&gt;, that is, if it is equal to &lt;code&gt;a[l],a[l+1],...,a[r]&lt;/code&gt; for some &lt;code&gt;(l,r)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; nums = [4,2,4,5,6]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 17&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The optimal subarray here is [2,4,5,6].&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; nums = [5,2,1,2,5,2,1,2,5]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 8&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The optimal subarray here is [5,2,1] or [1,2,5].&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;1 &amp;lt;= nums.length &amp;lt;= 105&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;1 &amp;lt;= nums[i] &amp;lt;= 104&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;SOLUTION:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def maximumUniqueSubarray(self, nums: List[int]) -&amp;gt; int:
        n = len(nums)
        msum = 0
        p = [0]
        for el in nums:
            p.append(p[-1] + el)
        prev = {}
        i = 0
        for j in range(n):
            if nums[j] in prev:
                i = max(i, prev[nums[j]] + 1)
            msum = max(msum, p[j + 1] - p[i])
            prev[nums[j]] = j
        return msum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>theabbie</category>
    </item>
    <item>
      <title>Balance a Binary Search Tree</title>
      <dc:creator>Abhishek Chaudhary</dc:creator>
      <pubDate>Sun, 19 Jun 2022 01:30:42 +0000</pubDate>
      <link>https://forem.com/theabbie/balance-a-binary-search-tree-29i1</link>
      <guid>https://forem.com/theabbie/balance-a-binary-search-tree-29i1</guid>
      <description>&lt;p&gt;Given the &lt;code&gt;root&lt;/code&gt; of a binary search tree, return &lt;em&gt;a &lt;strong&gt;balanced&lt;/strong&gt; binary search tree with the same node values&lt;/em&gt;. If there is more than one answer, return &lt;strong&gt;any of them&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A binary search tree is &lt;strong&gt;balanced&lt;/strong&gt; if the depth of the two subtrees of every node never differs by more than &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c_ViwiMq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://assets.leetcode.com/uploads/2021/08/10/balance1-tree.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c_ViwiMq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://assets.leetcode.com/uploads/2021/08/10/balance1-tree.jpg" alt="" width="714" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; root = [1,null,2,null,3,null,4,null,null]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [2,1,3,null,null,null,4]&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; This is not the only correct answer, [3,1,4,null,2] is also correct.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T-zN1ACJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://assets.leetcode.com/uploads/2021/08/10/balanced2-tree.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T-zN1ACJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://assets.leetcode.com/uploads/2021/08/10/balanced2-tree.jpg" alt="" width="224" height="145"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; root = [2,1,3]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [2,1,3]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The number of nodes in the tree is in the range &lt;code&gt;[1, 104]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;1 &amp;lt;= Node.val &amp;lt;= 105&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;SOLUTION:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# 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 inorder(self, root):
        if root:
            self.inorder(root.left)
            self.tree.append(root.val)
            self.inorder(root.right)

    def arrToTree(self, arr, i, j):
        if i &amp;lt;= j - 1:
            root = TreeNode()
            mid = (i + j) // 2
            root.val = arr[mid]
            root.left = self.arrToTree(arr, i, mid)
            root.right = self.arrToTree(arr, mid + 1, j)
            return root
        return None

    def balanceBST(self, root: TreeNode) -&amp;gt; TreeNode:
        self.tree = []
        self.inorder(root)
        return self.arrToTree(self.tree, 0, len(self.tree))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>theabbie</category>
    </item>
    <item>
      <title>Smallest String With Swaps</title>
      <dc:creator>Abhishek Chaudhary</dc:creator>
      <pubDate>Sun, 19 Jun 2022 01:00:46 +0000</pubDate>
      <link>https://forem.com/theabbie/smallest-string-with-swaps-2nof</link>
      <guid>https://forem.com/theabbie/smallest-string-with-swaps-2nof</guid>
      <description>&lt;p&gt;You are given a string &lt;code&gt;s&lt;/code&gt;, and an array of pairs of indices in the string &lt;code&gt;pairs&lt;/code&gt; where &lt;code&gt;pairs[i] = [a, b]&lt;/code&gt; indicates 2 indices(0-indexed) of the string.&lt;/p&gt;

&lt;p&gt;You can swap the characters at any pair of indices in the given &lt;code&gt;pairs&lt;/code&gt; &lt;strong&gt;any number of times&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Return the lexicographically smallest string that &lt;code&gt;s&lt;/code&gt; can be changed to after using the swaps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; s = "dcab", pairs = [[0,3],[1,2]]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "bacd"&lt;br&gt;
&lt;strong&gt;Explaination:&lt;/strong&gt; &lt;br&gt;
Swap s[0] and s[3], s = "bcad"&lt;br&gt;
Swap s[1] and s[2], s = "bacd"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; s = "dcab", pairs = [[0,3],[1,2],[0,2]]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "abcd"&lt;br&gt;
&lt;strong&gt;Explaination:&lt;/strong&gt; &lt;br&gt;
Swap s[0] and s[3], s = "bcad"&lt;br&gt;
Swap s[0] and s[2], s = "acbd"&lt;br&gt;
Swap s[1] and s[2], s = "abcd"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; s = "cba", pairs = [[0,1],[1,2]]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; "abc"&lt;br&gt;
&lt;strong&gt;Explaination:&lt;/strong&gt; &lt;br&gt;
Swap s[0] and s[1], s = "bca"&lt;br&gt;
Swap s[1] and s[2], s = "bac"&lt;br&gt;
Swap s[0] and s[1], s = "abc"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;1 &amp;lt;= s.length &amp;lt;= 10^5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;0 &amp;lt;= pairs.length &amp;lt;= 10^5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;0 &amp;lt;= pairs[i][0], pairs[i][1] &amp;lt; s.length&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;s&lt;/code&gt; only contains lower case English letters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;SOLUTION:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from collections import defaultdict

class Solution:
    def dfs(self, graph, node, visited):
        for j in graph[node]:
            if j not in visited:
                visited.add(j)
                self.dfs(graph, j, visited)

    def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -&amp;gt; str:
        op = list(s)
        graph = defaultdict(list)
        for a, b in pairs:
            graph[a].append(b)
            graph[b].append(a)
        globalvisited = set()
        for x in graph:
            if x not in globalvisited:
                currvisited = {x}
                self.dfs(graph, x, currvisited)
                indexes = sorted(currvisited)
                indexes_asc = sorted(currvisited, key = lambda p: s[p])
                n = len(indexes)
                for i in range(n):
                    op[indexes[i]] = s[indexes_asc[i]]
                globalvisited.update(currvisited)
        return "".join(op)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>theabbie</category>
    </item>
    <item>
      <title>Crawler Log Folder</title>
      <dc:creator>Abhishek Chaudhary</dc:creator>
      <pubDate>Sun, 19 Jun 2022 00:30:36 +0000</pubDate>
      <link>https://forem.com/theabbie/crawler-log-folder-6d5</link>
      <guid>https://forem.com/theabbie/crawler-log-folder-6d5</guid>
      <description>&lt;p&gt;The Leetcode file system keeps a log each time some user performs a &lt;em&gt;change folder&lt;/em&gt; operation.&lt;/p&gt;

&lt;p&gt;The operations are described below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;"../"&lt;/code&gt; : Move to the parent folder of the current folder. (If you are already in the main folder, &lt;strong&gt;remain in the same folder&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;"./"&lt;/code&gt; : Remain in the same folder.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;"x/"&lt;/code&gt; : Move to the child folder named &lt;code&gt;x&lt;/code&gt; (This folder is &lt;strong&gt;guaranteed to always exist&lt;/strong&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You are given a list of strings &lt;code&gt;logs&lt;/code&gt; where &lt;code&gt;logs[i]&lt;/code&gt; is the operation performed by the user at the &lt;code&gt;ith&lt;/code&gt; step.&lt;/p&gt;

&lt;p&gt;The file system starts in the main folder, then the operations in &lt;code&gt;logs&lt;/code&gt; are performed.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the minimum number of operations needed to go back to the main folder after the change folder operations.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iK3QcfMn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://assets.leetcode.com/uploads/2020/09/09/sample_11_1957.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iK3QcfMn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://assets.leetcode.com/uploads/2020/09/09/sample_11_1957.png" alt="" width="800" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; logs = ["d1/","d2/","../","d21/","./"]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Use this change folder operation "../" 2 times and go back to the main folder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4SOJo-fr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://assets.leetcode.com/uploads/2020/09/09/sample_22_1957.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4SOJo-fr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://assets.leetcode.com/uploads/2020/09/09/sample_22_1957.png" alt="" width="800" height="340"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; logs = ["d1/","d2/","./","d3/","../","d31/"]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 3&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; logs = ["d1/","../","../","../"]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 0&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;1 &amp;lt;= logs.length &amp;lt;= 103&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;2 &amp;lt;= logs[i].length &amp;lt;= 10&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;logs[i]&lt;/code&gt; contains lowercase English letters, digits, &lt;code&gt;'.'&lt;/code&gt;, and &lt;code&gt;'/'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;logs[i]&lt;/code&gt; follows the format described in the statement.&lt;/li&gt;
&lt;li&gt;  Folder names consist of lowercase English letters and digits.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;SOLUTION:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def minOperations(self, logs: List[str]) -&amp;gt; int:
        ctr = 0
        for log in logs:
            if log == "../":
                ctr =  max(ctr - 1, 0)
            elif log != "./":
                ctr += 1
        return ctr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>theabbie</category>
    </item>
    <item>
      <title>Remove Palindromic Subsequences</title>
      <dc:creator>Abhishek Chaudhary</dc:creator>
      <pubDate>Sun, 19 Jun 2022 00:00:45 +0000</pubDate>
      <link>https://forem.com/theabbie/remove-palindromic-subsequences-2hmj</link>
      <guid>https://forem.com/theabbie/remove-palindromic-subsequences-2hmj</guid>
      <description>&lt;p&gt;You are given a string &lt;code&gt;s&lt;/code&gt; consisting &lt;strong&gt;only&lt;/strong&gt; of letters &lt;code&gt;'a'&lt;/code&gt; and &lt;code&gt;'b'&lt;/code&gt;. In a single step you can remove one &lt;strong&gt;palindromic subsequence&lt;/strong&gt; from &lt;code&gt;s&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;the &lt;strong&gt;minimum&lt;/strong&gt; number of steps to make the given string empty&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A string is a &lt;strong&gt;subsequence&lt;/strong&gt; of a given string if it is generated by deleting some characters of a given string without changing its order. Note that a subsequence does &lt;strong&gt;not&lt;/strong&gt; necessarily need to be contiguous.&lt;/p&gt;

&lt;p&gt;A string is called &lt;strong&gt;palindrome&lt;/strong&gt; if is one that reads the same backward as well as forward.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; s = "ababa"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; s is already a palindrome, so its entirety can be removed in a single step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; s = "abb"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; "abb" -&amp;gt; "bb" -&amp;gt; "". &lt;br&gt;
Remove palindromic subsequence "a" then "bb".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; s = "baabb"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; "baabb" -&amp;gt; "b" -&amp;gt; "". &lt;br&gt;
Remove palindromic subsequence "baab" then "b".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;1 &amp;lt;= s.length &amp;lt;= 1000&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;s[i]&lt;/code&gt; is either &lt;code&gt;'a'&lt;/code&gt; or &lt;code&gt;'b'&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;SOLUTION:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def removePalindromeSub(self, s: str) -&amp;gt; int:
        if s == s[::-1]:
            return 1
        return 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>theabbie</category>
    </item>
    <item>
      <title>Asteroid Collision</title>
      <dc:creator>Abhishek Chaudhary</dc:creator>
      <pubDate>Sat, 18 Jun 2022 23:30:38 +0000</pubDate>
      <link>https://forem.com/theabbie/asteroid-collision-acf</link>
      <guid>https://forem.com/theabbie/asteroid-collision-acf</guid>
      <description>&lt;p&gt;We are given an array &lt;code&gt;asteroids&lt;/code&gt; of integers representing asteroids in a row.&lt;/p&gt;

&lt;p&gt;For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.&lt;/p&gt;

&lt;p&gt;Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; asteroids = [5,10,-5]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [5,10]&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The 10 and -5 collide resulting in 10. The 5 and 10 never collide.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; asteroids = [8,-8]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; []&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The 8 and -8 collide exploding each other.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; asteroids = [10,2,-5]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [10]&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;2 &amp;lt;= asteroids.length &amp;lt;= 104&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;-1000 &amp;lt;= asteroids[i] &amp;lt;= 1000&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;asteroids[i] != 0&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;SOLUTION:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def asteroidCollision(self, asteroids: List[int]) -&amp;gt; List[int]:
        n = len(asteroids)
        stack = []
        survivors = set()
        for i in range(n):
            if asteroids[i] &amp;gt; 0:
                stack.append(i)
            else:
                while len(stack) &amp;gt; 0 and abs(asteroids[stack[-1]]) &amp;lt; abs(asteroids[i]):
                    stack.pop()
                if len(stack) &amp;gt; 0:
                    if abs(asteroids[stack[-1]]) &amp;lt;= abs(asteroids[i]):
                        stack.pop()
                else:
                    survivors.add(i)
        for i in stack:
            survivors.add(i)
        return [asteroids[i] for i in range(n) if i in survivors]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>theabbie</category>
    </item>
    <item>
      <title>Check Whether Two Strings are Almost Equivalent</title>
      <dc:creator>Abhishek Chaudhary</dc:creator>
      <pubDate>Sat, 18 Jun 2022 23:00:46 +0000</pubDate>
      <link>https://forem.com/theabbie/check-whether-two-strings-are-almost-equivalent-pce</link>
      <guid>https://forem.com/theabbie/check-whether-two-strings-are-almost-equivalent-pce</guid>
      <description>&lt;p&gt;Two strings &lt;code&gt;word1&lt;/code&gt; and &lt;code&gt;word2&lt;/code&gt; are considered &lt;strong&gt;almost equivalent&lt;/strong&gt; if the differences between the frequencies of each letter from &lt;code&gt;'a'&lt;/code&gt; to &lt;code&gt;'z'&lt;/code&gt; between &lt;code&gt;word1&lt;/code&gt; and &lt;code&gt;word2&lt;/code&gt; is &lt;strong&gt;at most&lt;/strong&gt; &lt;code&gt;3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Given two strings &lt;code&gt;word1&lt;/code&gt; and &lt;code&gt;word2&lt;/code&gt;, each of length &lt;code&gt;n&lt;/code&gt;, return &lt;code&gt;true&lt;/code&gt; &lt;em&gt;if&lt;/em&gt; &lt;code&gt;word1&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; &lt;code&gt;word2&lt;/code&gt; &lt;em&gt;are &lt;strong&gt;almost equivalent&lt;/strong&gt;, or&lt;/em&gt; &lt;code&gt;false&lt;/code&gt; &lt;em&gt;otherwise&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;frequency&lt;/strong&gt; of a letter &lt;code&gt;x&lt;/code&gt; is the number of times it occurs in the string.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; word1 = "aaaa", word2 = "bccb"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; There are 4 'a's in "aaaa" but 0 'a's in "bccb".&lt;br&gt;
The difference is 4, which is more than the allowed 3.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; word1 = "abcdeef", word2 = "abaaacc"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The differences between the frequencies of each letter in word1 and word2 are at most 3:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;'a' appears 1 time in word1 and 4 times in word2. The difference is 3.&lt;/li&gt;
&lt;li&gt;'b' appears 1 time in word1 and 1 time in word2. The difference is 0.&lt;/li&gt;
&lt;li&gt;'c' appears 1 time in word1 and 2 times in word2. The difference is 1.&lt;/li&gt;
&lt;li&gt;'d' appears 1 time in word1 and 0 times in word2. The difference is 1.&lt;/li&gt;
&lt;li&gt;'e' appears 2 times in word1 and 0 times in word2. The difference is 2.&lt;/li&gt;
&lt;li&gt;'f' appears 1 time in word1 and 0 times in word2. The difference is 1.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; word1 = "cccddabba", word2 = "babababab"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The differences between the frequencies of each letter in word1 and word2 are at most 3:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;'a' appears 2 times in word1 and 4 times in word2. The difference is 2.&lt;/li&gt;
&lt;li&gt;'b' appears 2 times in word1 and 5 times in word2. The difference is 3.&lt;/li&gt;
&lt;li&gt;'c' appears 3 times in word1 and 0 times in word2. The difference is 3.&lt;/li&gt;
&lt;li&gt;'d' appears 2 times in word1 and 0 times in word2. The difference is 2.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;n == word1.length == word2.length&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;1 &amp;lt;= n &amp;lt;= 100&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;word1&lt;/code&gt; and &lt;code&gt;word2&lt;/code&gt; consist only of lowercase English letters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;SOLUTION:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from collections import Counter

class Solution:
    def checkAlmostEquivalent(self, word1: str, word2: str) -&amp;gt; bool:
        ctr1 = Counter(word1)
        ctr2 = Counter(word2)
        for c in "abcdefghijklmnopqrstuvwxyz":
            if abs(ctr1[c] - ctr2[c]) &amp;gt; 3:
                return False
        return True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>leetcode</category>
      <category>dsa</category>
      <category>theabbie</category>
    </item>
  </channel>
</rss>
