Leetcode: Interleaving String
来源:互联网 发布:des算法c语言实现 编辑:程序博客网 时间:2024/05/01 14:06
Given s1, s2, s3, find whether s3 is formed by the interleaving ofs1 and s2.
For example,
Given:
s1 = "aabcc"
,
s2 = "dbbca"
,
When s3 = "aadbbcbcac"
, return true.
When s3 = "aadbbbaccc"
, return false.
public class Solution { public boolean isInterleave(String s1, String s2, String s3) {// Start typing your Java solution below// DO NOT write main() functionint n1 = s1.length();int n2 = s2.length();int n3 = s3.length();if(n1 + n2 != n3)return false;boolean[][] ans = new boolean[n1 + 1][n2 + 1];// ans[i][j] == true means s3.substring(0, i + j) is an interleaving stringans[0][0] = true;for(int i = 1; i <= n1; i++){if(s1.charAt(i - 1) == s3.charAt(i - 1))ans[i][0] = true;elsebreak;}for(int i = 1; i <= n2; i++){if(s2.charAt(i - 1) == s3.charAt(i - 1))ans[0][i] = true;elsebreak;}for(int i = 1; i <= n1; i++){for(int j = 1; j <= n2; j++){if(s1.charAt(i - 1) == s3.charAt(i + j - 1))ans[i][j] = ans[i][j] || ans[i - 1][j];if(s2.charAt(j - 1) == s3.charAt(i + j - 1))ans[i][j] = ans[i][j] || ans[i][j - 1];}}return ans[n1][n2];}}
- 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
- scott用户sql操作
- windows安装PHP5.4+Apache2.4+Mysql5.5
- Could not open ServletContext resource [/WEB-INF/applicationContext.xml]解决方法
- 两个版本的oracle,装在一台主机上
- Sublime text 3如何安装package control办法
- Leetcode: Interleaving String
- 各大网站架构总结笔记(续)(转)
- 各大网站架构分析
- 黑马程序员---多线程
- win7(64)未在本地计算机上注册 Microsoft.Jet.OLEDB.4.0 提供程序
- ubuntu安装英伟达显卡GT 540M驱动
- 2012-2013下学期八年级期末考试成绩公布表
- Qt 4.7.1的移植[未上图]
- VMware安装WMware tools