Rifts Posted March 26, 2012 Share Posted March 26, 2012 Hey guys! I have a word list but I'm not really sure where to start to create an anagram solver. A push in the right direction would be great! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/259748-how-to-go-about-creating-an-anagram-solver/ Share on other sites More sharing options...
Psycho Posted March 26, 2012 Share Posted March 26, 2012 You could look into finding a permutation script (there have been many posts on these forums) to generate all the different ordering of the letters. Then compare those to the possible words. Although, if you have a target of words, then you could come up with a solution that counts the different letters and can compare that way. That would probably be more efficient. So, if the anagram and the target word have the same number of 'a', 'r', 's', etc. Then it is a solution. I'd probably order the letters in the words from a-z and then compare. Quote Link to comment https://forums.phpfreaks.com/topic/259748-how-to-go-about-creating-an-anagram-solver/#findComment-1331256 Share on other sites More sharing options...
guinbRo Posted March 26, 2012 Share Posted March 26, 2012 If all you're trying to do is figure out if two words are anagrams of each other, sorting the words a-z will work really well. If you're trying to print a valid anagram, then as has been suggested provide a word list. I'd maybe create an array then like this: array( "abt" = > array( "bat", "tab" ), "enst" = > array( "sent", "tens", "nets" ) ); That way the key is a sorted version of all the possible words you want. Quote Link to comment https://forums.phpfreaks.com/topic/259748-how-to-go-about-creating-an-anagram-solver/#findComment-1331258 Share on other sites More sharing options...
Rifts Posted March 26, 2012 Author Share Posted March 26, 2012 Thanks these are great ideas the only problem is the mixed up word has extra letters. This is where I'm getting stuck. Say the word is bat but the input is sfboart, and the user knows the word is 3 letters. I'm getting confused because the extra characters. Quote Link to comment https://forums.phpfreaks.com/topic/259748-how-to-go-about-creating-an-anagram-solver/#findComment-1331265 Share on other sites More sharing options...
guinbRo Posted March 27, 2012 Share Posted March 27, 2012 I'm not sure if this necessarily makes it a true anagram. Still, sort the words a-z and what you could do is use preg_match with the word to see if it is included in the input. Quote Link to comment https://forums.phpfreaks.com/topic/259748-how-to-go-about-creating-an-anagram-solver/#findComment-1331421 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.