【leetcode刷题笔记】Interleaving String

Given s1s2s3, 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问题。

用数组dp[i][j]记录取s1[1,i]和s2[1,j]交叉拼接出s3[1,i+j]。然后我们可以从此时s3最后一个字符的来源分出两个子问题。

  • 如果s3最后一个字符来源于s2,那么我们只要考察用s1[1,i]和s2[1,j-1]能否交叉拼接出s3[1,i+j-1]这个子问题。比如s1 = "aa", s2 = "b", s3 = "aab",此时s3最后一个字符"b"来源于s2,那么我们只要考察s1 = "aa" 和 s2 = ""能否交叉拼接出s3 = "aa"即可。
  • 如果s3最后一个字符来源于s1,那么同理,我们只要考察用s[1,i-1]和s2[1,j]能否交叉拼接出s3[1,i+j-1]这个子问题。

于是有递推式如下:

dp[i][j] = (dp[i-1][j] && s1[i-1] == s3[i+j-1]) || (dp[i][j-1] && s2[j-1] == s3[i+j-1])

dp的初始化也十分简单,考虑s1为空(dp第一行),只用s2匹配s3;或者,s2为空(dp第一列),只用s1匹配s3的情形即可。

代码如下:

 1 public class Solution {
 2     public boolean isInterleave(String s1, String s2, String s3) {
 3         int len1 = s1.length();
 4         int len2 = s2.length();
 5         int len3 = s3.length();
 6         if(len3 != len1 + len2)
 7             return false;
 8
 9         boolean[][] dp = new boolean[len1+1][len2+1];
10         dp[0][0] = true;
11
12         for(int i = 1;i<=len2;i++){
13             if(s2.charAt(i-1) == s3.charAt(i-1))
14                 dp[0][i]= true;
15             else {
16                 break;
17             }
18         }
19
20         for(int i = 1;i<=len1;i++){
21             if(s1.charAt(i-1) == s3.charAt(i-1))
22                 dp[i][0]= true;
23             else
24                 break;
25         }
26
27         for(int i = 1;i<= len1;i++){
28             char ch1 = s1.charAt(i-1);
29             for(int j = 1;j<=len2;j++){
30                 int k = i+j;
31                 char ch2 = s2.charAt(j-1);
32                 char ch3 = s3.charAt(k-1);
33                 if(ch1 == ch3)
34                     dp[i][j] = dp[i][j] | dp[i-1][j];
35                 if(ch2 == ch3)
36                     dp[i][j]= dp[i][j] | dp[i][j-1];
37             }
38         }
39
40         return dp[len1][len2];
41     }
42 }

【leetcode刷题笔记】Interleaving String

时间: 2024-08-10 23:28:36

【leetcode刷题笔记】Interleaving String的相关文章

【leetcode刷题笔记】String to Integer (atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

【leetcode刷题笔记】Scramble String

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choose a

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Multiply Strings

Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 题解:就是让实现一个大整数乘法. 假设两个数num1和num2的长度分别是len1和len2,那么最后得到的答案,在最高位有进位的时候,就是len1+len2位,否则是len1+len2

【leetcode刷题笔记】Substring with Concatenation of All Words

You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters. For example, given:S: "b

【leetcode刷题笔记】Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 题解:以strs[0]为模板,每次挨个查看是否所有的串里面是否第i位上都和strs[0]一样,如果都一样,把i位置上的字符放到answer里面,i++,继续循环,否则返回当前的answer. 代码如下: 1 public class Solution { 2 public String longestCommonPrefix

【leetcode刷题笔记】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 configuration of

【leetcode刷题笔记】Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". Note:If there is no such window i

【leetcode刷题笔记】Valid Number

Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true 题解:题目不难,就是有点麻烦,要注意的地方很多,总结一下: 前面和后面的空格要用s.trim()去掉: 前导的'+'和'-'号需要忽略: