Runtime error while submitting, which works fine in case of custom input : but fails when I try to submit solution.


  • 0
    P
    struct Node{
        public:
        bool word;
        Node* next[26];
        
        Node(bool b=false){
            word=b;
            memset(next,0,sizeof(26));
        }
        
    };
    
    class Trie{
        
        Node *root;
        public:
        
        Trie(vector<string> words)
        {
            root=new Node();
            for(int i=0;i<words.size();i++)
                insert(words[i]);
        }
    
        
        Node *getroot(){return root;}
        
          void insert(string s)
        {
            Node *curr=root;
            for(int i=0;i<s.size();i++)
            {
                if(!curr->next[s[i]-'a'])
                curr->next[s[i]-'a']=new Node();
                
                curr=curr->next[s[i]-'a'];
            }
              curr->word=true;
            
        }
    };
      
        
    
    
    class Solution {
    public:
        
        
        void dfs(vector<vector<char>>& board,int x,int y, string str, vector<string> &res,set<string> &s,Node *root)
        {
            static int arrx[]={0,0,-1,1};
            static int arry[]={-1,1,0,0};
      
            
            if(x<0||x>=board.size()||y<0||y>=board[0].size() || board[x][y]=='#') 
                return;
            
                
                if(!root->next[board[x][y]-'a'])
                    return;
                   
                 root=root->next[board[x][y]-'a'];
                
              
                char c='#';
                str=str+board[x][y];
                swap(board[x][y],c);
                
                
            
                if(root->word)
                {
                    s.insert(str);
                    root->word=false;
                }
                
            
                for(int k=0;k<4;k++)
                {
                    int newx=arrx[k]+x;
                    int newy=arry[k]+y;
                   
                         dfs(board,newx,newy,str,res,s,root);
                }
            
             swap(board[x][y],c);
             str = str.substr(0, str.length() - 1);
              
            
                    
                
        
        }
        
        
        vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
            
            string str="";
            vector<string> res;
          set<string> s;
            
            Trie *trie=new Trie(words);
            Node *root=trie->getroot();
            
            //memset(vis,false,sizeof(vis));
            
            for(int i=0;i<board.size();i++)
            {
                for(int j=0;j<board[0].size();j++)
                {
                  
                    dfs(board,i,j,str,res,s,root);
                    
                }
            }
            
            for(auto c:s)
                res.push_back(c);
            
            return res;
            
        }
        
    };
    
    
    
    

Log in to reply
 

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