Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all,
I have an input string "HELLOWORLD". I want to get all the possible words formed by the letters the given string using javascript.

like way,

Input string "Hello wolrd"

out strings:

Hello
hell
world
red
word
low
...
..
..

Please let me know
thanks in advance..
Posted

Agree that you need to do this yourself, however, since I have done something similar recently I'll give you a starter for 10, using VB.Net but should be translatable to other languages, such as the JavaScript you requested. There are no doubt better ways to do this, a DAWG would be the most efficient but probably pretty challenging to implement.

Get get one of more of the following lexicons (dictionaries without definitions), SOWPODS (Official Scrabble Words + Offical Scrabble Players Dictionary), ENABLE (Enhanced North American Benchmark LExicon) and TWL (Tournament Word List). As a bonus research the history of these Lexicons.

Create a class (or structure), let's call it Words, with these fields, Word as String, Length as Integer, alphWord as String. The constructor should take a single word (string) and calculate the length and it's alphabetically organised equivalent (alphWord).

Create a List(Of Words), call it MasterList.

Loop through one or more of those word lists, adding each distinct word to the List.

Create another List(Of Words), call it FilteredList.

Use the FindAll function of the List class to load the FilteredList from the MasterList. You will need to supply the Predicate(Of T) function that FindAll requires, i.e. write it yourself.

To help you the FindAll function loops through the MasterList, applying the function you write to each member of the List and if the function returns True it adds that member to the FilteredList, so by the time it finishes looping through the MasterList the FilteredList will only contain those that match your criteria in the function.

To help you a little further with the Predicate(Of T) function, there's a reason the Words class has a calculated alphWord field.
To make it operate faster through this looping exercise (those lexicons contain between 150k and 250k words each) there's a reason the Words class has a length field.

I managed to run through all 3 lists, about 600k words, in about 2-5 seconds.
As a bonus you could implement it in a Form that displays the MasterList in a DataGridView, takes any word you supply from a TextBox and populates another DataGridView with the FilteredList.

How you translate that to JavaScript is up to you.

Mike
 
Share this answer
 
Comments
timw 21-Oct-12 23:11pm    
should rename the site to pseudocodeproject.com
M-Badger 22-Oct-12 4:19am    
What was your expectation?
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
Try it yourself, you may find it is not as difficult as you think!
 
Share this answer
 

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