Advertisement

Interleaving String - LeetCode 97 Solution

Interleaving String - Complete Solution Guide

Interleaving String is LeetCode problem 97, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given strings s1 , s2 , and s3 , find whether s3 is formed by an interleaving of s1 and s2 . An interleaving of two strings s and t is a configuration where s and t are divided into n and m substrings respectively, such that: s = s 1 + s 2 + ... + s n t = t 1 + t 2 + ... + t m |n - m| <= 1 The interleaving is s 1 + t 1 + s 2 + t 2 + s 3 + t 3 + ... or t 1 + s 1 + t 2 + s 2 + t 3 + s 3 + ... Note: a + b is the concatenation of strings a and b . Example 1: Input: s1 = "aabcc", s2 = "dbbca", s3 = "

Detailed Explanation

The problem asks whether a string `s3` can be formed by interleaving two other strings, `s1` and `s2`. Interleaving means combining `s1` and `s2` such that their characters appear in `s3` while maintaining their original order within themselves. For example, if `s1` is "abc" and `s2` is "def", a possible interleaving `s3` would be "adbecf", "abdecf", or "abcdef". The lengths of `s1` and `s2` must sum up to the length of `s3`.

Solution Approach

The provided code uses dynamic programming with space optimization. A 1D boolean array `dp` of size `len2 + 1` is used. `dp[j]` represents whether the prefix of length `i+j` of `s3` can be formed by interleaving the prefix of length `i` of `s1` and the prefix of length `j` of `s2`. The algorithm iterates through all possible lengths of prefixes of `s1` and `s2`, updating the `dp` array based on whether the current characters of `s1` or `s2` match the corresponding character of `s3`. The final result is stored in `dp[len2]`, which indicates whether the entire string `s3` can be formed by interleaving `s1` and `s2`.

Step-by-Step Algorithm

  1. Step 1: Check if the sum of the lengths of `s1` and `s2` equals the length of `s3`. If not, return `false` because interleaving is impossible.
  2. Step 2: Initialize a 1D boolean array `dp` of size `len2 + 1` with all elements set to `false`. `dp[j]` represents if `s3[0...i+j-1]` is an interleaving of `s1[0...i-1]` and `s2[0...j-1]`.
  3. Step 3: Iterate through all possible lengths `i` of the prefix of `s1` from 0 to `len1` (inclusive).
  4. Step 4: For each `i`, iterate through all possible lengths `j` of the prefix of `s2` from 0 to `len2` (inclusive).
  5. Step 5: Calculate the value of `dp[j]` based on the following cases:
  6. - If `i == 0` and `j == 0`: Set `dp[j]` to `true` (empty strings interleave to form an empty string).
  7. - If `i == 0`: Set `dp[j]` to `dp[j-1] && s2[j-1] == s3[j-1]` (only `s2` contributes to `s3`).
  8. - If `j == 0`: Set `dp[j]` to `dp[j] && s1[i-1] == s3[i-1]` (only `s1` contributes to `s3`).
  9. - Otherwise: Set `dp[j]` to `(dp[j] && s1[i-1] == s3[i+j-1]) || (dp[j-1] && s2[j-1] == s3[i+j-1])`. This means `s3[i+j-1]` can come from either the last character of `s1` or the last character of `s2`.
  10. Step 6: After the iterations, return `dp[len2]`. This value indicates whether `s3` can be formed by interleaving `s1` and `s2`.

Key Insights

  • Insight 1: Dynamic Programming (DP) is suitable for this problem because it exhibits optimal substructure. The problem can be broken down into smaller overlapping subproblems: can a substring of `s3` be formed by interleaving substrings of `s1` and `s2`?
  • Insight 2: A 2D DP table (which we optimize to a 1D array) can efficiently store the results of these subproblems. The DP table represents whether a prefix of `s3` can be formed by interleaving a prefix of `s1` and a prefix of `s2`.
  • Insight 3: The current state of the DP table depends only on the previous state (the row or column above/left). This allows us to reduce the space complexity from O(m*n) to O(min(m, n)) by using only one row/column for DP calculation, thereby addressing the follow up.

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(n)

Topics

This problem involves: String, Dynamic Programming.

Companies

Asked at: Amazon, Apple, DE Shaw, Goldman Sachs, Microsoft, Nuro, TikTok, Uber, Walmart Labs, Yahoo, Zeta, thoughtspot.