Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey
Does anybody know of a method of how you can check for duplicate permutations of a word?

I.e if I input the characters TSTE one of the permutations will be SETT. my problem comes in on the fact that the output gives me SETT twice(x2), I want to build in a check that keeps that from happening. This is because of the use of 2 T's in the word and it sees it as a different word.

Any help will be greatly appreciated.

For you to see how I am finding my permutations at the moment.
Here is my code until now:

static void Main(string[] args)
        {
            Console.BufferHeight = Int16.MaxValue - 1;

            Console.WindowHeight = 40;
            Console.WindowWidth = 120;
            Console.WriteLine("Enter your caracters for the anagram: ");
            //var d = Read();
            string line;
            //string DictionaryInput = System.IO.File.ReadAllText("Dictionary.txt");
            while ((line = Console.ReadLine()) != null)
            {
                Console.WriteLine("Your results are: ");
                char[] charArray = line.ToArray();
                //Show(d, line);                    //Using this to check that words found are the correct words in the dictionary.
                setper(charArray);
                Console.WriteLine("-----------------------------------------DONE-----------------------------------------");
                Console.WriteLine("Enter your caracters for the anagram: ");
                File.Delete("Permutations.txt");
            }

        }
     


        static void swap(ref char a, ref char b)
        {
            if (a == b) return;
            a ^= b;
            b ^= a;
            a ^= b;
        }
        static void setper(char[] list)
        {
            int x = list.Length - 1;
            permuteWords(list, 0, x);
        }
        static void permuteWords(char[] list1, int k, int m)
        {

            if (k == m)
            {
                StreamWriter sw = new StreamWriter("Permutations.txt", true);
                sw.WriteLine(list1);
                sw.Close();
                Regex permutationPattern = new Regex(new string(list1));
                string[] permutations = File.ReadAllLines("Permutations.txt");
                Regex pattern = new Regex(new string(list1));
                string[] lines = File.ReadAllLines("Dictionary.txt");

                foreach (string line in lines)
                {
                    var matches = pattern.Matches(line);
                    if (pattern.ToString() == line)
                    {
                        Console.WriteLine(line);
                    }
                }
            }
            else
            {
                for (int i = k; i <= m; i++)
                {
                    swap(ref list1[k], ref list1[i]);
                    permuteWords(list1, k + 1, m);
                    swap(ref list1[k], ref list1[i]);
                }
            }


        }
Posted
Updated 6-Mar-13 1:18am
v3

1 solution

Depends how you are doing it really - there are loads of ways, starting with checking if it's in your list when you add new elements, up to using Linq to return only Distinct values.

We can't really give you a "one-size fits all" version, but the Linq method version is very simple:
C#
List<string> listOfPermutations = new List<string>();
listOfPermutations.Add("SETT");
listOfPermutations.Add("STET");
listOfPermutations.Add("SETT");
Console.WriteLine("Before");
foreach (string s in listOfPermutations)
    {
    Console.WriteLine(s);
    }
listOfPermutations = listOfPermutations.Distinct().ToList();
Console.WriteLine("After");
foreach (string s in listOfPermutations)
    {
    Console.WriteLine(s);
    }

Output:
Before
SETT
STET
SETT
After
SETT
STET
 
Share this answer
 
Comments
Prasad_Kulkarni 6-Mar-13 5:57am    
Nice one! +5
BulletVictim 6-Mar-13 6:24am    
Thanks I'll give it a try and see if it works with my code
BulletVictim 6-Mar-13 7:44am    
Unfortunately it does not seem to want to work all that well with my current code. But i will keep it in mind if I come accross something like this again. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900