Could someone give some suggestion about my code?


  • 0
    S

    public class Solution {
    public static boolean isInterleave(String s1, String s2, String s3) {

        int l1=s1.length(), l2=s2.length(),l3=s3.length(),i=0, j=0,m=0,n=0;
    
        if(l1==0||l2==0){
            return l1==0?s3==s2:s3==s1;
        }
        if(l3!=(l1+l2))return false;
        if(l3==0)return true;
        if((s3.charAt(0)!=s1.charAt(0))&&(s3.charAt(0)!=s2.charAt(0)))return false;
            
      for(int t=0;t<l1;t++){
            if(s3.charAt(t)==s1.charAt(i)){
                i++;
                m++;
            }
            else{
                break;
            }
        }
        
        for(int t=0;t<l2;t++){
          if(s3.charAt(t)==s2.charAt(j)){
              j++;
              n++;
          }
          else{
              break;
          }
            
        }
        
        return m>=n?Helper(s2,s1.substring(m),s3.substring(m)):Helper(s1,s2.substring(n),s3.substring(n));
        }
        
     public static boolean Helper(String s1,String s2,String s3){
         int l=s1.length(),l3=s3.length(),i=0,t=0;
         System.out.println(s1+" "+s2+" "+s3);
         if(l==0){
             return s3==s2;
         }
         if((s3.charAt(0)!=s1.charAt(0)))return false;
         while(i<l&&(s1.charAt(i)==s3.charAt(t))){
             i++;
             t++;
         }
         s3=s3.substring(t);
         s1=s1.substring(t);
         return Helper(s2,s1,s3);
     }
    

    }


  • 0
    S

    It gave the wrong answer in leetcode, but right answer in eclipse with the same example
    "a","","a"


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.