【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 ...
随机推荐
- Qt之可重入与线程安全
简述 本篇文章中,术语"可重入性"和"线程安全"被用来标记类与函数,以表明它们如何被应用在多线程应用程序中. 一个线程安全的函数可以同时被多个线程调用,甚至调用 ...
- java多线程下如何调用一个共同的内存单元(调用同一个对象)
/* * 关于线程下共享相同的内存单元(包括代码与数据) * ,并利用这些共享单元来实现数据交换,实时通信与必要的同步操作. * 对于Thread(Runnable target)构造方法创建的线程, ...
- 简述 Ruby 与 DSL 在 iOS 开发中的运用
阅读本文不需要预先掌握 Ruby 与 DSL 相关的知识 何为 DSL DSL(Domain Specific Language) 翻译成中文就是:"领域特定语言".首先,从定义就 ...
- 网页 console的使用
通过按下回车键会触发执行命令,而有时候我们需要执行的逻辑比较复杂,需要多行才可以完成,可以通过点击“shift+回车键”来实现换行. 在console中,可以实现对按钮的监控.比如此时按钮的文本值为“ ...
- BZOJ1722 [Usaco2006 Mar] Milk Team Select 产奶比赛
直接树形dp就好了恩 令$f[i][j][t]$表示以$i$为根的子树,选出来的点存在$j$对父子关系,$t$表示$i$这个点选或者没选,的最大产奶值 分类讨论自己和儿子分别有没有选,然后转移一下就好 ...
- Centos 6.2 安装mysql5.5
1. 安装mysql 相关依赖库(没有的话就安装,有就不用安装了) 通过 rpm -qa | grep name 的方式验证以下软件包是否已全部安装. gcc* gcc-c++* autoconf* ...
- 部分SIM卡被曝存安全漏洞:7.5亿部手机受牵连
7月22日消息,据国外媒体报道,一安全研究人员发现部分移动SIM卡所使用的加密方式存在一个安全漏洞,可能会导致手机被黑客远程控制. DES数据加密标准的SIM卡——DES是一种较旧的标准,目前正被部分 ...
- svn cleanup failed问题解决
1.SVN出错 今早过来Update,报如下错误: 再次更新,svn会要求你执行clean up,但执行clean up仍会报错,说有未完的work item,还要求你执行clean up.汗,陷入死 ...
- vim的编码设置
VIM的相关字符编码主要有三个参数 fencs: 它是一个编码格式的猜测列表.当用vim打开某个文件时,会依次取这里面的编码进行解码,如果某个编码格式从头至尾解码正确,那么就用那个编码 fenc:它是 ...
- [处理器、单片机]ARM
1.ARM简介: ARM是Advanced RISC Machines的缩写.1985年4月26日,第一个ARM原型在英国剑桥的Acorn计算机有限公司诞生,由美国加州San Jose VLSI技术公 ...