leetcode 68: Interleaving String
来源:互联网 发布:去哪找数据库 编辑:程序博客网 时间:2024/05/01 11:44
Interleaving StringAug 31 '12
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.
[backtracking] TLD
public class Solution { public boolean isInterleave(String s1, String s2, String s3) { //init check if(s1==null || s2==null || s3==null) return false; if(s1.length() + s2.length() != s3.length()) return false; return rec(s1, s2, s3); } private boolean rec(String s1, String s2, String s3) { if(s1.length()==0 && s2.length()==0) return s3.length()==0; else if(s1.length()==0) return s2.equals(s3); else if(s2.length()==0) return s1.equals(s3); if(s1.charAt(0)==s3.charAt(0) && rec(s1.substring(1), s2, s3.substring(1))) return true; else if(s2.charAt(0) == s3.charAt(0) && rec(s1, s2.substring(1), s3.substring(1))) return true; else return false; }}
[DP]
public class Solution { public boolean isInterleave(String s1, String s2, String s3) { //args check if(s1==null || s2==null || s3==null) return false; if(s1.length() + s2.length() != s3.length()) return false; if(s1.length() == 0) return s2.equals(s3); if(s2.length() == 0) return s1.equals(s3); int n1 = s1.length(); int n2 = s2.length(); boolean b[][] = new boolean[n2+1][n1+1]; for(int j=1; j<=n1; j++) { if(s1.charAt(j-1) == s3.charAt(j-1) ) b[0][j]=true; else break; } for(int i=1; i<=n2; i++) { if(s2.charAt(i-1) == s3.charAt(i-1) ) b[i][0]=true; else break; } for(int i=1; i<=n2; i++) { for(int j=1; j<=n1; j++) { if(b[i-1][j] && s2.charAt(i-1)==s3.charAt(i+j-1)) b[i][j] = true; else if(b[i][j-1] && s1.charAt(j-1)==s3.charAt(i+j-1)) b[i][j] = true; } } return b[n2][n1]; }}
- leetcode 68: Interleaving String
- LeetCode: Interleaving String
- LeetCode Interleaving String
- LeetCode: Interleaving String
- [Leetcode] Interleaving String
- [LeetCode] Interleaving String
- [Leetcode] Interleaving String
- LeetCode - Interleaving String
- [LeetCode]Interleaving String
- LeetCode: Interleaving String
- Leetcode: Interleaving String
- [Leetcode]Interleaving String
- Leetcode: Interleaving String
- LeetCode:Interleaving String
- LeetCode-Interleaving String
- [leetcode] Interleaving String@DP
- LeetCode:Interleaving String
- LeetCode - Interleaving String
- Xen的源码安装及dom0,domU的设置
- 女子为父筹钱治病舍身遭骗色究竟该怪谁?
- 这段耻辱的经历让我再也无法快乐
- Oracle trace & tkprof
- Oracle无法启动1——ORA-12541:TNS:无监听程序
- leetcode 68: Interleaving String
- 最近工作中的技术总结分享
- Oracle无法启动2——监听程序当前无法识别连接描述符中请求的服务
- iptables 1.4.17版本编译
- 春运正当时 古代人们又是靠什么出行的呢?
- life小语14
- 软件版本代号
- 得到 jQuery detach() 后节点中的某个值
- 计算机图形学---学习笔记