zoukankan      html  css  js  c++  java
  • Java for LeetCode 097 Interleaving String 【HARD】

    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.

    解题思路:

    DP问题,JAVA实现如下:

    	static public boolean isInterleave(String s1, String s2, String s3) {
    		if (s1.length() + s2.length() != s3.length())
    			return false;
    		boolean[] dp = new boolean[s2.length() + 1];
    		dp[0] = true;
    
    		for (int j = 1; j <= s2.length(); j++)
    			dp[j] = dp[j - 1] && s2.charAt(j - 1) == s3.charAt(j - 1);
    
    		for (int i = 1; i <= s1.length(); i++) {
    			dp[0] = dp[0] && s1.charAt(i - 1) == s3.charAt(i - 1);
    			for (int j = 1; j <= s2.length(); ++j) {
    				dp[j] = (dp[j] && s1.charAt(i - 1) == s3.charAt(i + j - 1))
    						|| (dp[j - 1] && s2.charAt(j - 1) == s3.charAt(i + j
    								- 1));
    			}
    		}
    		return dp[s2.length()];
    	}
    
  • 相关阅读:
    一周内签到连续天数求解
    int型动态数组总结
    快速排序总结
    希尔排序总结
    冒泡排序的总结
    桶排序总结
    插入排序的总结
    选择排序的总结
    二分法的五种实现
    安装Yum源
  • 原文地址:https://www.cnblogs.com/tonyluis/p/4520807.html
Copyright © 2011-2022 走看看