97. Interleaving String Medium

@problem@discussion
#String#Dynamic Programming



1/**
2 * [97] Interleaving String
3 *
4 * Given strings s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2.
5 * An interleaving of two strings s and t is a configuration where s and t are divided into n and m non-empty substrings respectively, such that:
6 * 
7 * 	s = s1 + s2 + ... + sn
8 * 	t = t1 + t2 + ... + tm
9 * 	|n - m| <= 1
10 * 	The interleaving is s1 + t1 + s2 + t2 + s3 + t3 + ... or t1 + s1 + t2 + s2 + t3 + s3 + ...
11 * 
12 * Note: a + b is the concatenation of strings a and b.
13 *  
14 * Example 1:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2020/09/02/interleave.jpg" style="width: 561px; height: 203px;" />
16 * Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
17 * Output: true
18 * Explanation: One way to obtain s3 is:
19 * Split s1 into s1 = "aa" + "bc" + "c", and s2 into s2 = "dbbc" + "a".
20 * Interleaving the two splits, we get "aa" + "dbbc" + "bc" + "a" + "c" = "aadbbcbcac".
21 * Since s3 can be obtained by interleaving s1 and s2, we return true.
22 * 
23 * Example 2:
24 * 
25 * Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
26 * Output: false
27 * Explanation: Notice how it is impossible to interleave s2 with any other string to obtain s3.
28 * 
29 * Example 3:
30 * 
31 * Input: s1 = "", s2 = "", s3 = ""
32 * Output: true
33 * 
34 *  
35 * Constraints:
36 * 
37 * 	0 <= s1.length, s2.length <= 100
38 * 	0 <= s3.length <= 200
39 * 	s1, s2, and s3 consist of lowercase English letters.
40 * 
41 *  
42 * Follow up: Could you solve it using only O(s2.length) additional memory space?
43 * 
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/interleaving-string/
48// discuss: https://leetcode.com/problems/interleaving-string/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52impl Solution {
53    pub fn is_interleave(s1: String, s2: String, s3: String) -> bool {
54        false
55    }
56}
57
58// submission codes end
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_97() {
66    }
67}
68


Back
© 2025 bowen.ge All Rights Reserved.