【LeetCode OJ】Interleaving String
Problem Link:
http://oj.leetcode.com/problems/interleaving-string/
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
For example,
Given:
s1 = "aabcc"
,
s2 = "dbbca"
,
When s3 = "aadbbcbcac"
, return true.
When s3 = "aadbbbaccc"
, return false.
This problem can solved by using DFS on a binary tree. The tree node has three integers (p1,p2,p3), which denotes that s3[0..p3-1] is the interleaving of s1[0..p1-1] and s2[0..p2-1]. For each node, there are following choices:
- If p3 == len(s3), it follows that s3 is the interleaving of s1 and s2 (we need assume that len(s3) = len(s1)+len(s2)), return True
- If p1 < len(s1) and s1[p1] == s3[p3], then s3[0..p3] could be the interleaving of s1[0..p1] and s2[0..p2-1], so we can go deeper to the node (p1+1, p2, p3+1);
- If p2 < len(s2) and s2[p2] == s3[p3], then s3[0..p3] could be the interleaving of s1[0..p1-1] and s2[0..p2], so we can go deeper to the node (p1, p2+1, p3+1);
- Otherwise, we cannot go deeper and this path will be terminated.
If we check all possible paths using DFS, and there is no successful path, we would return False and terminate the program. The python code should be as follows.
class Solution:
# @return a boolean
def isInterleave(self, s1, s2, s3):
n1 = len(s1)
n2 = len(s2)
n3 = len(s3)
if n1 + n2 != n3:
return False
# DFS
q = []
q.append( (0,0,0) ) # checked length for s1, s2, s3
while q:
l1, l2, l3 = q.pop()
# If checked length of s3 equals to length of s3, then return True
if l3 == n3:
return True
# If availabe char in s1:
if l1 < n1 and s1[l1] == s3[l3]:
q.append((l1+1,l2,l3+1))
# If available char in s2:
if l2 < n2 and s2[l2] == s3[l3]:
q.append((l1,l2+1,l3+1))
return False
I used build-in structure list implementing the Queue structure. However, the leetcode judging system returns timeout for this DFS implementation. I think it will be worse if you use recursive function instead of queue to carry out the DFS.
So I have to solve it in a more efficient way, I choose DP. I define a boolean 2D array A[0..n1][0..n2] to denote if s3[0:i+j] is the interleaving of s1[0:i] and s2[0:j]. The recursive formula for this DP solution is:
A[0][0] = True, since "" is the interleaving of "" and ""
A[0][j] = True, if s2[0:j] == s3[0:j], which means s3 is the interleaving of "" and s2 if s2 == s3
A[i][0] = True, if s1[0:i] == s3[0:i], which means s3 is the interleaving of s1 and "" if s1 == s3
A[i][j] = True if 1) A[i-1][j] = True and s1[i-1] == s3[i+j-1]; or 2) A[i][j-1] = True and s2[j-1] == s3[i+j-1], for 0<i<=n1, 0<j<=n2
A[][] is (n1+1)*(n2+1) array where A[i][j] means s3[0:i+j] is the interleaving of s1[0:i] and s2[0:j]. We can fill the A-table in a bottom-up way and just return A[n1][n2] to see if s3 is the interleaving of s1 and s2. The python code is as follows, which accepted by the leetcode OJ system.
class Solution:
# @return a boolean
def isInterleave(self, s1, s2, s3):
# A[i][j] means s1[0:i] and s2[0:j] is interleaves of s3[0:i+j]
# A[0][0] = True, since "" and "" are interleaves of ""
# A[0][j] = True, if s2[0:j] == s3[0:j]
# A[i][0] = True, if s1[0:i] == s3[0:j]
# A[i][j] = True, if any of following is true:
# 1) A[i-1][j] = True and s1[i-1] == s3[i+j-1]
# 2) A[i][j-1] = True and s2[j-1] == s3[i+j-1]
n1 = len(s1)
n2 = len(s2)
n3 = len(s3)
if n1 + n2 != n3:
return False
# Initialization
A = []
for _ in xrange(n1+1):
A.append([False]*(n2+1))
# Boundary conditions
A[0][0] = True
for j in xrange(n2):
if s2[j] == s3[j]:
A[0][j+1] = True
for i in xrange(n1):
if s1[i] == s3[i]:
A[i+1][0] = True
# Fill the table
for x in xrange(n1):
for y in xrange(n2):
i = x+1
j = y+1
if (A[i-1][j] and s1[i-1] == s3[i+j-1]) or (A[i][j-1] and s2[j-1] == s3[i+j-1]):
A[i][j] = True
return A[n1][n2]
【LeetCode OJ】Interleaving String的更多相关文章
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- 【LeetCode OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- 【LeetCode OJ】Word Ladder I
Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in t ...
- 【LeetCode OJ】Palindrome Partitioning II
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...
- 【LeetCode OJ】Palindrome Partitioning
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning/ We solve this problem using D ...
- 【LeetCode OJ】Valid Palindrome
Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would s ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- 【LeetCode OJ】Word Break
Problem link: http://oj.leetcode.com/problems/word-break/ We solve this problem using Dynamic Progra ...
随机推荐
- 小程序---根据数据库反向生成java文件
工作中写entry太繁琐,写了一个小程序反向生成.从而大大减少了工作量 import java.io.File; import java.io.FileWriter; import java.io.I ...
- win8 卸载IIS
C:\Windows\System32\inetsrv C:\Windows\iis7.log C:\inetpub
- 去除DataTable重复数据的三种方法
业务需求 最近做一个把源数据库的数据批次导出到目标数据库.源数据库是采集程序采集而来的原始数据库,所以需要对其进行一些处理(过滤一些为空,长度太短或太长,非法字符,重复数据)然后在进行入库. 其中要避 ...
- Web总结
Web总结 学习web前端理论基础必然是要过关的,这里我总结了一下比较基础的常用理论,还是比较有用哒! 一.名词解释 1.横切 在固定页面的宽度(按栅格化进行)并且对高度没有限制的容器称为一个标准横切 ...
- 读取Cookie及Cookie所有属性操作方法
读取Cookie及Cookie所有属性操作方法 2013-08-04 22:21:43| 分类: 技术 | 标签:cookie |举报|字号 订阅 要把Cookie发送到客户端,Servle ...
- Android中visibility属性VISIBLE、INVISIBLE、GONE的区别
详情见:http://blog.csdn.net/chindroid/article/details/8000713
- Andriod——区别DVM与JVM
区别DVM与JVM 1.首要差别 Dalvik: 基于寄存器,编译和运行都会更快些 JVM: 基于栈, 编译和运行都会慢些 2.字节码的区别 Dalvik: 执行.dex格式的字节码,是对.class ...
- C#如何以管理员身份运行程序
在使用winform程序获取调用cmd命令提示符时,如果是win7以上的操作系统,会需要必须以管理员身份运行才会执行成功,否则无效果或提示错误. 比如在通过winform程序执行cmd命令时,某些情况 ...
- HDU 4622 求解区间字符串中的不同子串的个数
题目大意: 给定一个长度<2000的串,再给最多可达10000的询问区间,求解区间字符串中的不同子串的个数 这里先考虑求解一整个字符串的所有不同子串的方法 对于后缀自动机来说,我们动态往里添加一 ...
- java基础之类与继承 详解
Java:类与继承 对于面向对象的程序设计语言来说,类毫无疑问是其最重要的基础.抽象.封装.继承.多态这四大特性都离不开类,只有存在类,才能体现面向对象编程的特点,今天我们就来了解一些类与继承的相关知 ...