Skip to main content

Posts

Leetcode 97: Interleaving String

  1. Problem Summary You’re given three strings s1, s2, and s3. Determine whether s3 can be formed by  interleaving all characters of s1 and s2 , preserving the relative order of characters from each string. You  cannot  reorder characters inside s1 or s2. At each step, you choose the next char either from s1 or from s2. All characters of s1 and s2 must be used exactly once. Formally, s3 is an interleaving of s1 and s2 if: len(s3) = len(s1) + len(s2), and There exists a merge of s1 and s2, preserving internal order of each, that equals s3. Input: s1, s2 with length up to 100 s3 with length up to 200 Output: Boolean: true if s3 is an interleaving of s1 and s2, else false. The “substring splitting” notation in the statement boils down to this standard definition: merge s1...

Leetcode 96: Unique Binary Search Trees

  1. Problem Summary You are given an integer n. Consider all binary search trees (BSTs) that: Have exactly n nodes. Use each value from 1 to n  exactly once . Obey the BST property: for every node, left subtree values < node value < right subtree values. Task:  Return the  number  of structurally unique BSTs you can form (you do not need to generate the trees themselves). Input:  n (1 ≤ n ≤ 19) Output:  An integer: count of unique BST structures. 2. Examples Explanation Example 1: Input:  n = 3 Output:  5 From problem 95, the 5 structures for values {1,2,3} are: Root 1: right subtree formed from {2,3} (2 possibilities) Root 2: left from {1}, right from {3} (1 possibility) Root 3: left subtree formed from {1,2} (2 possibilities) Total: 2 + 1 + 2 = 5. Example 2: Input:  n = 1 Only one node with value 1, so exactly one BST. Output:  1. 3. A...