Very Concise and simple solution Accepted with Queue not sure it still good


  • 0
    V

    public class Solution {

    internal class Data
    {
        public int index;
        public string value;
        
        public Data(int i, string s)
        {
            index= i;
            value =s;
        }
        
        
    }
    
    public IList<string> WordBreak(string s, IList<string> wordDict) {
        
      
        Queue<Data> queue = new Queue<Data>();
        IList<string> response = new List<string>();
        
        Data obj  = new Data(0,"");
        
        queue.Enqueue(obj);
    
        while(queue.Count >0 && s.Length <100)
        {
            Data data =  queue.Dequeue();
            int start =data.index;
          
            for(int end = start +1;end<= s.Length;end++)
            {
                string toCheck = s.Substring(start, end-start);
                
                 
                if(wordDict.Contains(toCheck))
                {
                    if(end ==s.Length)
                    {
                        response.Add(data.value + toCheck);
                    }
                    else
                    {
                           obj  = new Data(end,data.value + toCheck + " ");
                           queue.Enqueue(obj);
                    }
                   
                }
            }
        }
        
        return response;
    }
    

    }


Log in to reply
 

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