[Coding Made Simple] String Interleaving

本文探讨了如何判断一个字符串是否为另外两个字符串的交织串,并提供了递归及动态规划两种解决方案。

Given three strings, return true if third string is interleaving of first and second string.

 

By running some examples, it seems that as long as s1.length() + s2.length() == s3.length() and both s1 and s2 are common subsequences of s3, 

s3 is interleaving of s1 and s2. However, this assumption is not correct as shown in the following counter example.

s1 = "aba", s2 = "aca", s3 = "abacaa", both s1 and s2 are common subsequence of s3, but s3 is not interleaving of s1 and s2. The problem lies in 

that s1 and s2 have overlapping characters in the common subsequence portion of s3. 

 

Solution 1. Recursion

Because we need to keep the relative order of characters of s1 and s2 in s3, we know that for the last character of s3 must be either from s1's or s2's 

end if s3 is interleaving of s1 and s2. If this is not the case, we know for sure s3 is not interleaving of s1 and s2.

Based on the above observation, the following recursive formulas are derived. 

Base case: all characters in s3 are checked already, return true.

For s1[0.....i], s2[0....j], s3[0... i + j + 1], 

case 1: there are unused characters in s1, i >= 0 && s1[i] == s3[i + j + 1]

       check if s3[0....i + j] is interleaving of s1[0...i - 1] and s2[0....j];

if case 1 is false, proceed to case 2.

case 2: there are unused characters in s2, j >= 0 && s2[j] == s3[i + j + 1]

       check if s3[0....i + j] is interleaving of s1[0...i] and s2[0....j - 1];

if case 2 is false, we' just checked all possibilities, so return false.

 

Similarly with a lot of recursive solutions, this solution has the overlapping subproblems issue. 

For example,  for s1[0, 6] and s2[0, 5], we have the following recursive tree. 

                            s1[0,6] and s2[0,5]

              s1[0,5] and s2[0,5]                    s1[0, 6] and s2[0,4]

    s1[0,4] and s2[0, 5]      s1[0,5] and s2[0,4]           s1[0,5] and s2[0,4]      s1[0,6] and s2[0,4]

The red subproblems are overlapped.

 

 1 public class InterleavingString {
 2     public boolean isInterleavingStringRecursion(String s1, String s2, String s3) {
 3         if(s1.length() + s2.length() != s3.length()) {
 4             return false;
 5         }
 6         return recursiveHelper(s1, s1.length() - 1, s2, s2.length() - 1, s3, s3.length() - 1);
 7     }
 8     private boolean recursiveHelper(String s1, int endIdx1, String s2, int endIdx2, String s3, int endIdx3) {
 9         if(endIdx3 < 0) {
10             return true;
11         }
12         if(endIdx1 >= 0 && s1.charAt(endIdx1) == s3.charAt(endIdx3)) {
13             if(recursiveHelper(s1, endIdx1 - 1, s2, endIdx2, s3, endIdx3 - 1)) {
14                 return true;
15             }
16         }
17         else if(endIdx2 >= 0 && s2.charAt(endIdx2) == s3.charAt(endIdx3)) {
18             if(recursiveHelper(s1, endIdx1, s2, endIdx2 - 1, s3, endIdx3 - 1)) {
19                 return true;
20             }            
21         }
22         return false;
23     }
24     public static void main(String[] args) {
25         String s1 = "aab", s2 = "axy", s3 = "aaxaby", s4 = "abaaxy";
26         String s5 = "aabcc", s6 = "dbbca", s7 = "aadbbcbcac", s8 = "aadbbbaccc";
27         InterleavingString test = new InterleavingString();
28         System.out.println(test.isInterleavingStringRecursion(s1, s2, s3));
29         System.out.println(test.isInterleavingStringRecursion(s1, s2, s4));
30         System.out.println(test.isInterleavingStringRecursion(s5, s6, s7));
31         System.out.println(test.isInterleavingStringRecursion(s5, s6, s8));        
32     }
33 }

 

 

Solution 2. Dynamic Programming 

State: T[i][j]: whether s3[0 ~ i + j - 1] is interleaving of s1[0 ~ i - 1] and s2[0 ~ j - 1]

Function: same with the recursive solution.

Init: T[0][0] = true, as an empty string is interleaving of two empty strings 

 

 1 public boolean isInterleavingStringDp(String s1, String s2, String s3) {
 2     if(s1.length() + s2.length() != s3.length()) {
 3         return false;
 4     }
 5     boolean[][] T = new boolean[s1.length() + 1][s2.length() + 1];
 6     T[0][0] = true;
 7     for(int i = 1; i < T.length; i++) {
 8         T[i][0] = s1.charAt(i - 1) == s3.charAt(i - 1) && T[i - 1][0];
 9     }
10     for(int j = 1; j < T[0].length; j++) {
11         T[0][j] = s2.charAt(j - 1) == s3.charAt(j - 1) && T[0][j - 1];
12     }
13     for(int i = 1; i < T.length; i++) {
14         for(int j = 1; j < T[0].length; j++) {
15             if(s1.charAt(i - 1) == s3.charAt(i + j - 1) && T[i - 1][j]) {
16                 T[i][j] = true;
17             }
18             else if(s2.charAt(j - 1) == s3.charAt(i + j - 1) && T[i][j - 1]) {
19                 T[i][j] = true;
20             }
21             else {
22                 T[i][j] = false;
23             }
24         }
25     }
26     return T[s1.length()][s2.length()];
27 }

 

 

Related Problems 

Distinct Subsequence

转载于:https://www.cnblogs.com/lz87/p/7288851.html

(Mathcad+Simulink仿真)基于扩展描述函数法的LLC谐振变换器小信号分析设计内容概要:本文围绕“基于扩展描述函数法的LLC谐振变换器小信号分析设计”展开,结合Mathcad与Simulink仿真工具,系统研究LLC谐振变换器的小信号建模方法。重点利用扩展描述函数法(Extended Describing Function Method, EDF)对LLC变换器在非线性工作条件下的动态特性进行线性化近似,建立适用于频域分析的小信号模型,并通过Simulink仿真验证模型准确性。文中详细阐述了建模理论推导过程,包括谐振腔参数计算、开关网络等效处理、工作模态分析及频响特性提取,最后通过仿真对比验证了该方法在稳定性分析与控制器设计中的有效性。; 适合人群:具备电力电子、自动控制理论基础,熟悉Matlab/Simulink和Mathcad工具,从事开关电源、DC-DC变换器或新能源变换系统研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握LLC谐振变换器的小信号建模难点与解决方案;②学习扩展描述函数法在非线性系统线性化中的应用;③实现高频LLC变换器的环路补偿与稳定性设计;④结合Mathcad进行公式推导与参数计算,利用Simulink完成动态仿真验证。; 阅读建议:建议读者结合Mathcad中的数学推导与Simulink仿真模型同步学习,重点关注EDF法的假设条件与适用范围,动手复现建模步骤和频域分析过程,以深入理解LLC变换器的小信号行为及其在实际控制系统设计中的应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值