【Leetcode】Interleaving String
来源:互联网 发布:守望网络初始化失败 编辑:程序博客网 时间:2024/06/08 11:24
题目链接:
https://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.
思路:
1,DP,dp[i][2]表示s3从0~i若能表示为s1到s2的部分交叉字符串,则记录s1或s2对应的下标。
2,DP,将两字符展开为二维数组。判断是否能由左上角走到右下角。
算法2:
public boolean isInterleave(String s1, String s2, String s3) { boolean maps[][] = new boolean[s1.length() + 1][s2.length() + 1]; maps[0][0] = true; if(s1.length()+s2.length()!=s3.length()) return false; for (int i = 1; i < s1.length() + 1; i++) { maps[i][0] = maps[i - 1][0] && s3.charAt(i - 1) == s1.charAt(i - 1); } for (int j = 1; j < s2.length() + 1; j++) { maps[0][j] = maps[0][j - 1] && s3.charAt(j - 1) == s2.charAt(j - 1); } for (int i = 1; i < s1.length() + 1; i++) { for (int j = 1; j < s2.length() + 1; j++) { maps[i][j] = (maps[i][j - 1] && s3.charAt(i + j - 1) == s2.charAt(j - 1)) || (maps[i - 1][j] && s3.charAt(i + j - 1) == s1.charAt(i - 1)); } } return maps[s1.length()][s2.length()]; }
0 1
- LeetCode: Interleaving String
- LeetCode Interleaving String
- LeetCode: Interleaving String
- [Leetcode] Interleaving String
- [LeetCode] Interleaving String
- 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@DP
- LeetCode:Interleaving String
- LeetCode - Interleaving String
- Ubuntu安装使用SSH
- Redis源码解析:28集群(四)手动故障转移、从节点迁移
- 【Regex】初试Python下的中文正则表达式
- 上传的某些资源的效果图
- 802.11协议精读7:802.11a/g的发送过程与接收过程
- 【Leetcode】Interleaving String
- CopytOnWriteArrayList
- Android BGABadgeView:BGABadgeImageView以及BGABadgeRelativeLayout(4)
- JAVA-元数据
- Problem F
- 二叉排序树(或二叉查找树)的建立
- 减少Draw Call的方法
- 根据一个域名取其根域
- Add Digits