• Find all possible permutations of a string (C#)
    5 replies, posted
How can I find all possible permutations of a string, and return those permutations as a string array? I need it for the anagram solver I'm writing for class, and I've already got a dictionary look up function. [B]EDIT:[/b] Nevermind, i found a better way to do it.
I did a anagram solver a long time back, but I did it a little differently. I generated an array for the word with how many of the different letters eg. 2 a's, 0 b's, 1c, etc. Then I looked that array up in a dictionary to check if there were similar words.
Yeah you're better off counting the letters rather than creating permutations. But if you must, then do it recursively: [code] function permute(str) if str.length < 2 return [str] // [] denotes a list ret = [] // empty list for each letter in str for each entry in permute(str minus the current letter) ret += current letter + current entry return ret [/code] There's a better, iterative way of doing it, but its a bit more complicated.
[QUOTE=nullsquared;20090107]Yeah you're better off counting the letters rather than creating permutations. But if you must, then do it recursively: [code] function permute(str) if str.length < 2 return [str] // [] denotes a list ret = [] // empty list for each letter in str for each entry in permute(str minus the current letter) ret += current letter + current entry return ret [/code] There's a better, iterative way of doing it, but its a bit more complicated.[/QUOTE] That isn't C#, and I can't think of how to convert that to C#.
[QUOTE=Phyxius;20091506]That isn't C#, and I can't think of how to convert that to C#.[/QUOTE] Yeah it's pseudocode, and if you can't convert simple pseudocode to C# you need to go back and learn the basics.
[QUOTE=Phyxius;20091506]That isn't C#, and I can't think of how to convert that to C#.[/QUOTE] Are you joking?
Sorry, you need to Log In to post a reply to this thread.