Work on C# and does not work on Java


  • 0
    A

    Sorry for my english(and Java and C#)

    I'm not a java-coder, i'm using c#.
    for "aa", "*" this code on C# return true

    if (p.Length > s.Length)
    return false;

            char[] sA = s.ToCharArray();
            char[] pA = p.ToCharArray();
    
    
            int pi = 0;
    
            for (int i = 0; i < s.Length; i++)
            {
                if (i - pi >= p.Length)
                    return false;
    
                if (sA[i] != pA[i - pi] && pA[i - pi].ToString() != "?")
                {
                    if (pA[i - pi].ToString() != "*")
                        return false;
    
                    else
                    {
                        char workC = sA[i];
                        i++;
                        pi++;
                        for (i = i; i < s.Length; i++)
                        {
                            if (sA[i] != workC)
                                break;
                            pi++;
    
                        }
                    }
    
    
                }
    
    
            }
            return true;        
    

    but when i rewriting this one on Java is return false:

        if(p.length()>s.length())
        return false;
     
        
        char[] sA = s.toCharArray();
        char[] pA = p.toCharArray();
        
        
        int pi=0;
        
        for(int i=0;i<s.length();i++)
        {
            if(i-pi>=p.length())
            return false;
            
           if(sA[i]!=pA[i-pi]&& String.valueOf(pA[i-pi])!="?")
           {
            if(String.valueOf(pA[i-pi])!="*")
             return false;
             
             else
            {
                char workC = sA[i];
             i++;
             pi++;
             for(i=i;i<s.length();i++)
             {
                 if(sA[i]!=workC)
                  break;
                  pi++;
                 
             }
            }
             
               
           }
           
            
        }
    

    return true;


Log in to reply
 

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