Skip to content

Commit ac3a859

Browse files
committed
[LeetCode Sync] Runtime - 45 ms (55.31%), Memory - 18.5 MB (22.87%)
1 parent 7e0a006 commit ac3a859

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<p>Given strings <code>s1</code>, <code>s2</code>, and <code>s3</code>, find whether <code>s3</code> is formed by an <strong>interleaving</strong> of <code>s1</code> and <code>s2</code>.</p>
2+
3+
<p>An <strong>interleaving</strong> of two strings <code>s</code> and <code>t</code> is a configuration where <code>s</code> and <code>t</code> are divided into <code>n</code> and <code>m</code> <span data-keyword="substring-nonempty">substrings</span> respectively, such that:</p>
4+
5+
<ul>
6+
<li><code>s = s<sub>1</sub> + s<sub>2</sub> + ... + s<sub>n</sub></code></li>
7+
<li><code>t = t<sub>1</sub> + t<sub>2</sub> + ... + t<sub>m</sub></code></li>
8+
<li><code>|n - m| &lt;= 1</code></li>
9+
<li>The <strong>interleaving</strong> is <code>s<sub>1</sub> + t<sub>1</sub> + s<sub>2</sub> + t<sub>2</sub> + s<sub>3</sub> + t<sub>3</sub> + ...</code> or <code>t<sub>1</sub> + s<sub>1</sub> + t<sub>2</sub> + s<sub>2</sub> + t<sub>3</sub> + s<sub>3</sub> + ...</code></li>
10+
</ul>
11+
12+
<p><strong>Note:</strong> <code>a + b</code> is the concatenation of strings <code>a</code> and <code>b</code>.</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/02/interleave.jpg" style="width: 561px; height: 203px;" />
17+
<pre>
18+
<strong>Input:</strong> s1 = &quot;aabcc&quot;, s2 = &quot;dbbca&quot;, s3 = &quot;aadbbcbcac&quot;
19+
<strong>Output:</strong> true
20+
<strong>Explanation:</strong> One way to obtain s3 is:
21+
Split s1 into s1 = &quot;aa&quot; + &quot;bc&quot; + &quot;c&quot;, and s2 into s2 = &quot;dbbc&quot; + &quot;a&quot;.
22+
Interleaving the two splits, we get &quot;aa&quot; + &quot;dbbc&quot; + &quot;bc&quot; + &quot;a&quot; + &quot;c&quot; = &quot;aadbbcbcac&quot;.
23+
Since s3 can be obtained by interleaving s1 and s2, we return true.
24+
</pre>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> s1 = &quot;aabcc&quot;, s2 = &quot;dbbca&quot;, s3 = &quot;aadbbbaccc&quot;
30+
<strong>Output:</strong> false
31+
<strong>Explanation:</strong> Notice how it is impossible to interleave s2 with any other string to obtain s3.
32+
</pre>
33+
34+
<p><strong class="example">Example 3:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> s1 = &quot;&quot;, s2 = &quot;&quot;, s3 = &quot;&quot;
38+
<strong>Output:</strong> true
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
<p><strong>Constraints:</strong></p>
43+
44+
<ul>
45+
<li><code>0 &lt;= s1.length, s2.length &lt;= 100</code></li>
46+
<li><code>0 &lt;= s3.length &lt;= 200</code></li>
47+
<li><code>s1</code>, <code>s2</code>, and <code>s3</code> consist of lowercase English letters.</li>
48+
</ul>
49+
50+
<p>&nbsp;</p>
51+
<p><strong>Follow up:</strong> Could you solve it using only <code>O(s2.length)</code> additional memory space?</p>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
3+
m, n = len(s1), len(s2)
4+
if m + n != len(s3):
5+
return False
6+
7+
@cache
8+
def dfs(i: int, j: int) -> bool:
9+
if i >= m and j >= n:
10+
return True
11+
12+
k = + i + j
13+
if i < m and s1[i] == s3[k] and dfs(i + 1, j):
14+
return True
15+
if j < n and s2[j] == s3[k] and dfs(i, j + 1):
16+
return True
17+
return False
18+
19+
return dfs(0, 0)

0 commit comments

Comments
 (0)