Skip to content

Latest commit

 

History

History
135 lines (122 loc) · 5.75 KB

README.md

File metadata and controls

135 lines (122 loc) · 5.75 KB

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 = s1 + s2 + ... + sn
  • t = t1 + t2 + ... + tm
  • |n - m| <= 1
  • The interleaving is s1 + t1 + s2 + t2 + s3 + t3 + ... or t1 + s1 + t2 + s2 + t3 + s3 + ...

Note: a + b is the concatenation of strings a and b.

 

Example 1:

Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true
Explanation: One way to obtain s3 is:
Split s1 into s1 = "aa" + "bc" + "c", and s2 into s2 = "dbbc" + "a".
Interleaving the two splits, we get "aa" + "dbbc" + "bc" + "a" + "c" = "aadbbcbcac".
Since s3 can be obtained by interleaving s1 and s2, we return true.

Example 2:

Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output: false
Explanation: Notice how it is impossible to interleave s2 with any other string to obtain s3.

Example 3:

Input: s1 = "", s2 = "", s3 = ""
Output: true

 

Constraints:

  • 0 <= s1.length, s2.length <= 100
  • 0 <= s3.length <= 200
  • s1, s2, and s3 consist of lowercase English letters.

 

Follow up: Could you solve it using only O(s2.length) additional memory space?

Companies: Amazon, Adobe, Google

Related Topics:
String, Dynamic Programming

Solution 1. DP Top-down (DFS + Memo)

// OJ: https://leetcode.com/problems/interleaving-string/
// Author: github.com/lzl124631x
// Time: O(2^(M+N))
// Space: O(MN)
class Solution {
    int M, N;
    vector<vector<int>> m;
    int dfs(string &a, string &b, string &c, int i, int j) {
        if (i == M && j == N) return 1;
        if (m[i][j] != 0) return m[i][j];
        int val = -1;
        if (i < M && a[i] == c[i + j]) val = dfs(a, b, c, i + 1, j);
        if (val != 1 && j < N && b[j] == c[i + j]) val = dfs(a, b, c, i, j + 1);
        return m[i][j] = val;
    }
public:
    bool isInterleave(string s1, string s2, string s3) {
        M = s1.size(), N = s2.size();
        if (M + N != s3.size()) return false;
        m.assign(M + 1, vector<int>(N + 1));
        return dfs(s1, s2, s3, 0, 0) == 1;
    }
};

Solution 2. DP Bottom-up

Let dp[i][j] be whether a[0..(i-1)] and b[0..(j-1)] can form c[0..(i+j-1)].

dp[i][j] =    (i-1 >= 0 && a[i-1] == c[i+j-1] && dp[i-1][j])
           || (j-1 >= 0 && b[j-1] == c[i+j-1] && dp[i][j-1])

dp[0][0] = true
// OJ: https://leetcode.com/problems/interleaving-string/
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(MN)
class Solution {
public:
    bool isInterleave(string a, string b, string c) {
        int M = a.size(), N = b.size();
        if (c.size() != M + N) return false;
        vector<vector<bool>> dp(M + 1, vector<bool>(N + 1));
        dp[0][0] = true;
        for (int i = 0; i <= M; ++i) {
            for (int j = 0; j <= N; ++j) {
                if (i == 0 && j == 0) continue;
                dp[i][j] = (i >= 1 && a[i - 1] == c[i + j - 1] && dp[i - 1][j])
                        || (j >= 1 && b[j - 1] == c[i + j - 1] && dp[i][j - 1]);
            }
        }
        return dp[M][N];
    }
};

Solution 3. DP with Space Optimization

Since dp[i][j] is only dependent on dp[i-1][j] and dp[i][j-1], we can reduce the dp array from 2D to 1D.

// OJ: https://leetcode.com/problems/interleaving-string/
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(N)
class Solution {
public:
    bool isInterleave(string a, string b, string c) {
        int M = a.size(), N = b.size();
        if (c.size() != M + N) return false;
        vector<bool> dp(N + 1);
        dp[0] = true;
        for (int i = 0; i <= M; ++i) {
            for (int j = 0; j <= N; ++j) {
                if (i == 0 && j == 0) continue;
                dp[j] = (i >= 1 && a[i - 1] == c[i + j - 1] && dp[j]) || (j >= 1 && b[j - 1] == c[i + j - 1] && dp[j - 1]);
            }
        }
        return dp[N];
    }
};