Skip to content

Commit 33f3a67

Browse files
committed
leetcode.com 97. Interleaving String
문제 링크: https://leetcode.com/problems/interleaving-string
1 parent afee3f9 commit 33f3a67

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from collections import deque
2+
3+
4+
class Solution:
5+
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
6+
if len(s3) != (len(s1) + len(s2)):
7+
return False
8+
9+
s1, s2, s3 = ' ' + s1, ' ' + s2, ' ' + s3
10+
11+
dp = [[False for _ in range(len(s2))] for _ in range(len(s1))]
12+
dp[0][0] = True
13+
14+
for i1 in range(0, len(s1)):
15+
for i2 in range(0, len(s2)):
16+
if i2 - 1 >= 0 and dp[i1][i2 - 1]:
17+
if s3[i1 + i2] == s2[i2]:
18+
dp[i1][i2] = True
19+
20+
if i1 - 1 >= 0 and dp[i1 - 1][i2]:
21+
if s3[i1 + i2] == s1[i1]:
22+
dp[i1][i2] = True
23+
24+
return dp[len(s1) - 1][len(s2) - 1]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from unittest import TestCase
2+
from main import Solution
3+
4+
class TestSolution(TestCase):
5+
def test1_is_interleave(self):
6+
sln = Solution()
7+
self.assertEqual(True, sln.isInterleave(s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"))

0 commit comments

Comments
 (0)