0

So I'm using this code down here to figure out all the words that could be spelled out of the alphabet variable, the problem is , I build this alphabet variable each time I call this based on the board of random letters in front of the user. What i see though , and of course, is "aaab" for example...

What I'm after is for code to only use the letter as many times as it appears in the alphabet var, so that it can't do something like "aaab" but just "ab"

I understand this code that I found in another thread is made to build combinations of the letters into 4 letter words, or arrangements,

I'm wondering if theres a simple way using SelectMany or Select, to not add up its self if its already been used, keep in mind there could be multiple "a's" in the alphabet var to begin with, so if theres 2 A's in there, it should still be able to to AAB, just not AAAB. I am a newbie, I know that I could go through my own list and add letters together based on how many times they actually exist in the alphabet string..im just wondering if theres a way to interupt i or x and not add to q if its already been used...

sorry if this is confusing... thank you :)

// I found this in another thread and seemed to work great and fast.

var alphabet = "abcd";
var q = alphabet.Select(x => x.ToString());
int size = 4;
for (int i = 0; i < size - 1; i++)
  q = q.SelectMany(x => alphabet, (x, y) => x + y);

foreach (var item in q)
  ( DO STUFF)
  • There's a good series by eric lippert that you might find useful (and thoroughly enjoy, as I did): http://ericlippert.com/2013/04/15/producing-permutations-part-one/ – Steven Evers Feb 06 '15 at 17:20

2 Answers2

0

To reach your goal, you must find a way to mark letters in your alphabet which are already used and avoid using these letters a second time.

To do so you need a data structure which can store more than the letters alone, so a list of letters (or a string) is not sufficient.

Try to bulid a list of classes like this one:

class UsedLetter
{
    char letter;
    bool used;
}

Then you can mark each letter as used after you drew it from the list.


Improvement

You may also store your alphabet as a list of characters:

List<char> alphabet;

and remove each letter from the alphabet after its drawn.

DrKoch
  • 9,556
  • 2
  • 34
  • 43
  • Thank you for the fast reply. If the letter exists twice in the list? I guess I could track how many times the UsedLetter appears in the list and then check against that , if it appears multiple times. Hmmm thank you very much! – Nigel Metcalf Feb 06 '15 at 07:18
  • Well, with an albhabet like 'mama' you could draw 'a' two times, before both 's's are _used_ – DrKoch Feb 06 '15 at 07:25
  • I like the idea of removing each letter as it's used in that series of permutations. I'll try using a temp list to sort out as the big list of words is created. Thanks again. Just joined! Not enough cred to upvote anything yet! I will come back and do so when I can haha! – Nigel Metcalf Feb 06 '15 at 07:29
0

Here's how I have achieved what I think you're after:

using System;
using System.Collections.Generic;
using System.Linq;

namespace WordPerms
{
    class Program
    {
        Stack<char> chars = new Stack<char>();
        List<string> words = new List<string>();

        static void Main(string[] args)
        {
            Program p = new Program();
            p.GetChar("abad");

            foreach (string word in p.words)
            {
                Console.WriteLine(word);
            }
        }

// This is called recursively to build the list of words.
private void GetChar(string alpha)
        {
            string beta;

            for (int i = 0; i < alpha.Length; i++)
            {
                chars.Push(alpha[i]);
                beta = alpha.Remove(i, 1);
                GetChar(beta);
            }

            char[] charArray = chars.Reverse().ToArray();
            words.Add(new string(charArray));

            if (chars.Count() >= 1)
            {
                chars.Pop();
            }
        }
    }
}

Hope that helps, Greg.

IglooGreg
  • 147
  • 5
  • Thanks Greg , that totally does help! It's exactly what I was working on as I type this. What you've provided is simpler that what I was doing - thanks for the insight!! Once again, I will upvote as soon as I have enough rep to do so. – Nigel Metcalf Feb 06 '15 at 18:16