-2

My code here lists all the permutations of a string

{
    public partial class Form1 : Form
    {
        private int n;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            string scrambledWord = textBox1.Text;

            if (e.KeyCode == Keys.Enter)
            {
                label4.Text = "";
                char[] arr = scrambledWord.ToCharArray();
                GetPer(arr);
            }
        }

        public void GetPer(char[] list)
        {
            int x = list.Length - 1;
            GetPer(list, 0, x);
        }

        private void GetPer(char[] list, int k, int m)
        {
            if (k == m)
            {
                Console.WriteLine(list);
                var text = new string(list);
                label4.Text = label4.Text + text + Environment.NewLine;

            }
            else
                for (int i = k; i <= m; i++)
                {
                    Swap(ref list[k], ref list[i]);
                    GetPer(list, k + 1, m);
                    Swap(ref list[k], ref list[i]);
                }
        }
        private void Swap(ref char a, ref char b)
        {
            if (a == b) return;

            var temp = a;
            a = b;
            b = temp;
        }

        private void label1_TextChanged(object sender, EventArgs e)
        {
            label4.Height = label4.Height + 10;
        }








    }
}

Does anyone know how to check if any of the permutations are an English word and the code prints the English words on Visual Studio? Thank you‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎

  • 1
    Does this answer your question? [How to get english language word database?](https://stackoverflow.com/questions/2213607/how-to-get-english-language-word-database) – Charlieface Jan 01 '21 at 14:38
  • I'd use a [DAWG](http://www.wutka.com/dawg.html) to represent all the words. This way you can stop permutations that won't produce any valid words, potentially preventing thousands, or millions, of garbage words from being generated. For instance, can you think of any words that start with "gz"? – Idle_Mind Jan 02 '21 at 23:35

1 Answers1

0

You will need to add a text file with English vocabulary in your project (there are several such open-source lists, if you search for them, for example on GitHub). Add this file of words as a Content file to your project. Then read the list of words and search for the permutations in it. Because that is a lot of words, you can improve the speed of your app if you store the words in Hashset<string> because then you will be able to check for the permutation's existence in near O(1) time.

Follows is a pseudo-code of the solution:

var wordHashSet = new HashSet<string>();
var words = File.ReadAllLines("words.txt");
foreach (var word in words)
{
   wordHashSet.Add(word.ToUpperInvariant());
}

// checking for permutation

if (wordHashSet.Contains(permutation.ToUpperInvariant()))
{
   // the permutation is an english word
}
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91