13. Interleaved Strings
β GFG solution to the Interleaved Strings problem: determine if string s3 is formed by interleaving s1 and s2 while maintaining character order using dynamic programming. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given strings s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2.
Interleaving of two strings s1 and s2 is a way to mix their characters to form a new string s3, while maintaining the relative order of characters from s1 and s2.
Conditions for interleaving:
Characters from
s1must appear in the same order ins3as they are ins1.Characters from
s2must appear in the same order ins3as they are ins2.The length of
s3must be equal to the combined length ofs1ands2.
π Examples
Example 1
Input: s1 = "AAB", s2 = "AAC", s3 = "AAAABC"
Output: true
Explanation: The string "AAAABC" has all characters of the other two strings and in the same order.Example 2
Input: s1 = "AB", s2 = "C", s3 = "ACB"
Output: true
Explanation: s3 has all characters of s1 and s2 and retains order of characters of s1.Example 3
Input: s1 = "YX", s2 = "X", s3 = "XXY"
Output: false
Explanation: "XXY" is not interleaved of "YX" and "X". The strings that can be formed are YXX and XYX.π Constraints
$1 \le \text{s1.length, s2.length} \le 300$
$1 \le \text{s3.length} \le 600$
β
My Approach
The optimal approach uses Dynamic Programming with Space Optimization to efficiently determine if s3 can be formed by interleaving s1 and s2:
1D Dynamic Programming (Space Optimized)
Initial Check:
Verify if the total length of
s1ands2equals the length ofs3. If not, return false immediately.
DP Array Setup:
Use a 1D boolean array
dpof sizem+1(wheremis the length ofs2).dp[j]represents whether the firsticharacters ofs1and firstjcharacters ofs2can interleave to form the firsti+jcharacters ofs3.
Base Case:
dp[0] = true(empty strings can interleave to form empty string).Initialize first row: Check if
s2alone can form the beginning ofs3.
DP Transition:
For each position
(i, j), check two possibilities:Take character from
s1:dp[j]remains true if previous state was true AND currents1character matchess3.Take character from
s2:dp[j-1]was true AND currents2character matchess3.
Update
dp[j]using OR logic of both possibilities.
Result:
Return
dp[m]which indicates if entires1ands2can interleave to forms3.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n Γ m), where n is the length of
s1and m is the length ofs2. We fill a DP table of size n Γ m, processing each cell once.Expected Auxiliary Space Complexity: O(m), as we use a 1D array of size m+1 for space-optimized dynamic programming, avoiding the O(n Γ m) space of a full 2D DP table.
π§βπ» Code (C++)
class Solution {
public:
bool isInterleave(string &s1, string &s2, string &s3) {
if (s1.size() + s2.size() != s3.size()) return false;
int n = s1.size(), m = s2.size();
vector<bool> dp(m + 1);
dp[0] = true;
for (int j = 1; j <= m; j++) dp[j] = dp[j - 1] && s2[j - 1] == s3[j - 1];
for (int i = 1; i <= n; i++) {
dp[0] = dp[0] && s1[i - 1] == s3[i - 1];
for (int j = 1; j <= m; j++)
dp[j] = (dp[j] && s1[i - 1] == s3[i + j - 1]) || (dp[j - 1] && s2[j - 1] == s3[i + j - 1]);
}
return dp[m];
}
};β Code (Java)
class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if (s1.length() + s2.length() != s3.length()) return false;
int n = s1.length(), m = s2.length();
boolean[] dp = new boolean[m + 1];
dp[0] = true;
for (int j = 1; j <= m; j++) dp[j] = dp[j - 1] && s2.charAt(j - 1) == s3.charAt(j - 1);
for (int i = 1; i <= n; i++) {
dp[0] = dp[0] && s1.charAt(i - 1) == s3.charAt(i - 1);
for (int j = 1; j <= m; j++)
dp[j] = (dp[j] && s1.charAt(i - 1) == s3.charAt(i + j - 1)) || (dp[j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1));
}
return dp[m];
}
}π Code (Python)
class Solution:
def isInterleave(self, s1, s2, s3):
if len(s1) + len(s2) != len(s3): return False
n, m = len(s1), len(s2)
dp = [False] * (m + 1)
dp[0] = True
for j in range(1, m + 1): dp[j] = dp[j - 1] and s2[j - 1] == s3[j - 1]
for i in range(1, n + 1):
dp[0] = dp[0] and s1[i - 1] == s3[i - 1]
for j in range(1, m + 1):
dp[j] = (dp[j] and s1[i - 1] == s3[i + j - 1]) or (dp[j - 1] and s2[j - 1] == s3[i + j - 1])
return dp[m]π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated