leetCode 97.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对1的严格交错,后面才发现并不是,交错事字符串的个数是不确定的。

后面只好参考网上的资料,用动态规划求解。

2-dimension dp:

这是一个二维的动态规划,

s1 = "aabcc"

s2 = "dbbca"

s3 = "aadbbcbcac"

上面的图十分清楚,我也是在这个图的基础上理解并写出的代码。

代码如下:

public class Solution {
    public boolean isInterleave(String s1, String s2, String s3) {
    	/**
    	 * 动态规划求解
    	 * 参考资料:http://blog.csdn.net/doc_sgl/article/details/11714793
    	 */
    	int len1 = s1.length();
    	int len2 = s2.length();
    	int len3 = s3.length();

    	if(len1 + len2 != len3){
    		return false;
    	}

    	boolean[][] f = new boolean[len1+1][len2+1];
    	f[0][0] = true;//第一个设置为true
    	for(int i = 0; i < len1+1;i++){
    		for(int j = 0; j < len2+1; j++){
    			//i>0,j>0的条件是摒除i=0j=0的值
    			if(j > 0){
    				f[i][j] = f[i][j-1]&&(s3.charAt(i+j-1) == s2.charAt(j-1));
    			}
    			if(i > 0){
    				f[i][j] = f[i][j] || ( f[i-1][j]&&(s3.charAt(i+j-1) == s1.charAt(i-1)));
    			}
    		}
    	}
    	return f[len1][len2];
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-15 16:07:40

leetCode 97.Interleaving String (交错字符串) 解题思路和方法的相关文章

leetCode 22.Generate Parentheses (生成括号) 解题思路和方法

Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "

leetCode 44.Wildcard Matching (通配符匹配) 解题思路和方法

Wildcard Matching '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s,

leetCode 49.Anagrams (回文构词法) 解题思路和方法

Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 思路:这题要是解,必须知道什么是回文构词法.所谓回文构词法就是把一个单词的顺序调整,形成新的单词,如"eat","tea"就是回文构词法. 所以回文构词法一定是相同的字母不同的顺序,而且最少有两个单词. 本题是将单词排序之

leetCode 51.N-Queens (n皇后问题) 解题思路和方法

N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configur

leetCode 67.Add Binary (二进制加法) 解题思路和方法

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 思路:二进制加法,比较简单.代码如下: public class Solution { public String addBinary(String a, String b) { int len = Math.max(a.len

leetCode 91.Decode Ways (解码方式) 解题思路和方法

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encoded

leetCode 27.Remove Element (删除元素) 解题思路和方法

Remove Element Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 思路:此题和26题一脉相承,算法上不难,具体如代码所示: public class

leetCode 57.Insert Interval (插入区间) 解题思路和方法

Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], i

leetCode 39.Combination Sum(组合总和) 解题思路和方法

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includi