OJ not giving the expected answer?


  • 0
    G

    This is with regards to the test case

    Input:	"a", ["a"]
    Output:	[]
    Expected:	["a"]
    

    My programs give the expected output but it is shown as [] in OJ. Is there a problem with my code or some problem with OJ?

    class Solution {
    public:
    
        map<string,int> M;
        vector<string> x;
    
       // Function that finds all the sub-strings of s that are present in dict. 
       // Standard DP solution expect that I have used 'map' to store them.
    
        void wB(string s, unordered_set<string> &dict) {
    
            if(M.find(s)!=M.end())
               return ;
    
            int i,j;
            string t="",t1;
            for(i=0;i<s.size();i++)
            {
                t += s[i];
                if(dict.find(t)!=dict.end())
                    {
                        M[t]=1;
                        j=i+1;
                        t1 = s.substr(j,s.size()-j);
                        wB(t1,dict);
                    }
                else
                    M[t]=0;
            }
        }
    
       // Function to recover all possible word breaks using the map.
       
        void getall(string s, unordered_set<string> &dict, int pos, string s1)
        {
            //cout<<pos<<" "<<s1<<"\n";
            if(pos==s.size())
            {
                //cout<<"\t pushing "<<s1<<"\n";
                x.push_back(s1);
                return;
            }
            int i;
            string t="",olds1;
            for(i=pos;i<s.size();i++)
            {
                t += s[i];
                if(M[t]==1)
                {
                    olds1 = s1;
                    s1 == ""? s1 = t: s1+= ' '+t;
                    getall(s,dict,i+1,s1);
                    s1 = olds1;
                }
            }
    
        }
    
        vector<string> wordBreak(string s, unordered_set<string> &dict) {
    
            getall(s,dict,0,"");
            return x;
        }
    };
    

    I have left some commented code. Uncommenting them prints the expected output.


  • -1
    S

    You cannot cout anything in OJ, or will lead to unexpected judge result.


  • 0
    G

    I have not "cout" anything... they are commented, please check carefully.


  • 1
    G

    Problem fixed!

    Apologies. There was a line of code difference between OJ and my local machine. I had missed wB(s,dict);.

    vector<string> wordBreak(string s, unordered_set<string> &dict) {
    
        
        getall(s,dict,0,"");
        return x;
    }
    

    PS: the above solution still does not solve the judges requirement as it assumes that a valid break is always possible.


Log in to reply
 

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