-1

I am doing the following task (in c#): We have a group of letters and an english dictionary. Find all possible combinations of words that are created from the letters provided. To do that, I use trie data structure - I search for words and all possible additional words from remaining letters(recursive operation). However, the operation is very time/space consuming. Any idea how to handle it more efficiently ?

EDIT This is example code I prepared:

class Trie
    {
        private Node root = new Node(null);

        public void AddWord(string word)
        {
            root.Add(word, 0);
        }

        public void GetCandidates(string input)
        {
            var results = new List<Result>()
            {
                new Result() {Rest = input}
            };

            Get(results);
        }

        private void Get(List<Result> results)
        {
            foreach (var result in results.Where(r => !string.IsNullOrEmpty(r.Rest)).ToList())
            {
                var pattern = result.Rest.Replace(" ", string.Empty);

                var allWords = new List<Result>();
                root.GetWord(string.Empty, allWords, pattern);
                result.OhterWords = allWords;

                Get(allWords);
            }


        }
    }

    class Node
    {
        protected Dictionary<char,Node> children = new Dictionary<char, Node>();

        public bool End { get; private set; }

        public char? Key { get; private set; }

        public Node(char? key)
        {
            Key = key;
        }

        public void Add(string word, int index)
        {
            var letter = word[index];
            if (!children.ContainsKey(letter))
            {
                children.Add(letter, new Node(letter));

            }

            var nextIndex = index + 1;
            if (nextIndex < word.Length)
            {
                children[letter].Add(word, nextIndex);
            }
            else
            {
                children[letter].End = true;
            }
        }

        public virtual void GetWord(string current,  List<Result> allWords, string availableLetters)
        {
            var newCurrent = string.Concat(current, Key);
            if (End)
            {
                var result = new Result()
                {
                    Rest = availableLetters,
                    Word = newCurrent,
                };


                if (!allWords.Contains(result))
                {
                    allWords.Add(result);
                }
            }

            foreach (var letter in availableLetters)
            {
                if (children.ContainsKey(letter))
                {
                    var index = availableLetters.IndexOf(letter);
                    var tempAvailableString = availableLetters.Remove(index, 1);
                    children[letter].GetWord(newCurrent, allWords,  tempAvailableString);
                }
            }
        }
    }

    class Result
    {
        public List<Result> OhterWords { get; set; }

        public string Word { get; set; }

        public string Rest { get; set; }

        public override bool Equals(object obj)
        {
            var r = obj as Result;
            if (r == null)
            {
                return false;
            }

            return r.Word == Word && r.Rest == Rest;
        }
    }
macpak
  • 1,190
  • 1
  • 14
  • 28
  • can you provide some sample code to show us what you have at the moment? – Kevin Brady Jan 31 '15 at 17:58
  • 2
    You should post your code or at least explain the algorithm you're using a bit more clearly. We don't know why the operation is time/space consuming until we understand how you're performing the operation. – Jacob Jan 31 '15 at 17:58
  • Also, you mention space-consuming; could you show us how you're storing the trie? – Jacob Jan 31 '15 at 18:08
  • Instead of just asking us what you could do differently, please tell us what makes you unsatisfied with the code you have already, what parts of it you need to improve/fix, and why. – stakx - no longer contributing Feb 01 '15 at 15:57
  • Allright, so generaly I get OutOfMemoryException :) so we can say that the code simply doesn't work. The problem I've here is that I am not sure if I get the exception because the problem I am trying to solve can't be solved that way or I have bugs/insufficient operations inside my code. – macpak Feb 01 '15 at 16:05

1 Answers1

0

You can try aho-corasick algorithm. It uses a trie and also suffix and alternative trie data structures.

Micromega
  • 12,486
  • 7
  • 35
  • 72