Check whether string is interleaved
2010-10-01 09:59
405 查看
Three strings say A,B,C are given to you. Check whether 3rd string is interleaved from string A and B.
Ex: A="abcd" B="xyz" C="axybczd". answer is yes.
如果元素不相同的话,可以类似归并排序在0(N)时间内解决。
如果不想同的话,可以递归的解决
f(a, b, c)
{
if(c.size = 0 && a.size==0 &&b.size == 0) return 1;
else if(c.size == 0) return 0;
int ans = 0;
if(a[0] = c[0]) ans += f(a+1, b, c+1);
if(b[0] = c[0]) ans += f(a, b+1, c+1);
return ans > 0 ? 1: 0;
}
可以考虑加上个备忘录,减少重复递归
Ex: A="abcd" B="xyz" C="axybczd". answer is yes.
如果元素不相同的话,可以类似归并排序在0(N)时间内解决。
如果不想同的话,可以递归的解决
f(a, b, c)
{
if(c.size = 0 && a.size==0 &&b.size == 0) return 1;
else if(c.size == 0) return 0;
int ans = 0;
if(a[0] = c[0]) ans += f(a+1, b, c+1);
if(b[0] = c[0]) ans += f(a, b+1, c+1);
return ans > 0 ? 1: 0;
}
可以考虑加上个备忘录,减少重复递归
相关文章推荐
- DP33 两个字符串交叉得到的字符串 Find if a string is interleaved of two other strings @geeksforgeeks
- use regular expression check whether a string is a number
- How to Check Whether API Server is up or Down
- 1. PermCheck 桃花顺检验 Check whether array A is a permutation.
- leetCode Given an integer (signed 32 bits), write a function to check whether it is a power of 4
- How to Check Whether Oracle Binary/Instance is RAC Enabled and Relink Oracle Binary in RAC
- Check whether array A is a permutation.
- *Check whether a given graph is Bipartite or not
- How to Check Whether Oracle Binary/Instance is RAC Enabled and Relink Oracle Binary in RAC (文档 ID 28
- how to know whether one string is english string
- Geeks - Check whether a given graph is Bipartite or not 二分图检查
- Check if a string is NULL or EMPTY using PowerShell
- How to check a string is e-mail format
- How to Check Whether Oracle Binary/Instance is RAC Enabled and Relink Oracle Binary in RAC (文档 ID 28
- javax.servlet.jsp.JspException: Error - tag.getAsString : component context is not defined. Check tag syntax
- Checking Whether a String is Numeric
- 【Leetcode】Given a binary tree, check whether it is a mirror of itself
- find whether a given string is formed by the
- suse 11 sp4配置yast源报错cannot access installation media(Medium 1),Check whether the server is accessibl
- FW - check whether tree is balanced or not - find the max and min of one tree- 2013年12月18日13:12:04