首页 > 代码库 > interleaving-string

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.

 

 

 1 public class Solution { 2     public static void main(String[] args) 3     { 4         Solution s=new Solution(); 5         String s1="ab"; 6         String s2=""; 7         String s3="ba"; 8         System.out.println(s.isInterleave(s1, s2, s3)); 9     }10     11     public boolean isInterleave(String s1, String s2, String s3) {12        int len1=s1.length();13        int len2=s2.length();14        int len3=s3.length();15         16         if (s3==null||s3.length()==0)17             return false;18         19         if (len3!=len1+len2)20             return false;21         22         char[] ch1=s1.toCharArray();23         char[] ch2=s2.toCharArray();24         char[] ch3=s3.toCharArray();25         26         Boolean flag=isInterleave(ch1,len1-1,ch2,len2-1,ch3,len3-1);27         return flag;28     }29     30     public boolean isInterleave(char[] ch1,int len1,char[] ch2,int len2, char[] ch3,int len3)31     {32         Boolean result=false;33         char c1=‘ ‘,c2=‘ ‘,c3=‘ ‘;34         if(len1>=0)35         c1=ch1[len1];36         37         if(len2>=0)38         c2=ch2[len2];39         40         if(len3>=0)41         c3=ch3[len3];42         43         if (c3!=c1&&c3!=c2)44             return false;45         46         if(len3==1&&len1==1&&c3==c1)47         {48             return true;49         }50         51         if(len3==1&&len2==1&&c3==c2)52         {53             return true;54         }55         56         if (c3==c1&&c3!=c2)57         {58             result=isInterleave(ch1,len1-1,ch2,len2,ch3,len3-1);59         }60         61         if (c3==c2&&c3!=c1)62         {63             result=isInterleave(ch1,len1,ch2,len2-1,ch3,len3-1);64         }65         66         if(c3==c2&&c3==c1)67         {68             result=isInterleave(ch1,len1-1,ch2,len2,ch3,len3-1);69             if(result==false)70                 result=isInterleave(ch1,len1,ch2,len2-1,ch3,len3-1);71         }72         73         return result;74         75         76     }77 }

 

自己写的代码,在本地调没问题,但是提交到牛客网中显示运行错误,不知道怎么回事。(在牛客网提交时注意将main函数注释掉)

interleaving-string