问题
I'm trying to improve this anagrams generator in javascript:
JS:
var str,re,cnt,cntmax;
function rst(){
cnt=0; cntmax=10000; str="";
if (document.forms[0].re.value != ""){
re=new RegExp(document.forms[0].re.value);
} else { re=null; }
}
function go(prefix, postfix){
if (cnt>=cntmax) return;
if (postfix==""){
if (re==null || prefix.match(re)==null){
str+=prefix+"\r\n"; cnt++;
}
return;
}
for (var i=0;i<postfix.length;i++){
var prefix2=prefix+postfix.charAt(i);
var postfix2=postfix.substring(0,i)+postfix.substring(i+1);
go(prefix2,postfix2);
}
}
HTML:
<form>
Anagram this word:<input type="text" name="t" /> <br>
Exclude these pat:<input type="text" name="re" /> <br>
<input type="button" value="Generate anagrams (max 10000)"
onClick="javascript: rst(); go('', t.value); tout.value=str; console.log('Anagrams: '+cnt);" />
<br>
<textarea name="tout" rows=10 cols=40></textarea>
</form>
there are some problems to fix, for example need to a words separator (like a split) for the words to exclude,
but the most important thing is to make the generator find only words collected in a list (like a database)...
I have not idea how to creat that list, please help...
DEMO HERE
回答1:
Depending on the size of the list of words, you might be better off implementing this with server-side code where you have access to a database. But I'll answer you question assuming you really want to use a javascript client.
The simplest way would be to just have an array of words and check if each string is contained in the list. You can presort it and use a binary search if you want better performance, but this example will just use a simple linear search.
var dictionary = [
"apple",
"banana",
"orange",
"pear",
"watermelon"
];
function isValidWord(word) {
for(var i = 0, n = dictionary.length; i < n; i++) {
if(word === dictionary[i]) {
return true;
}
}
return false;
}
You'd then change this line:
if (re==null || prefix.match(re)==null){
to this:
if ((re==null || prefix.match(re)==null) && isValidWord(prefix) ){
Now, as I mentioned, this could be a performance problem with a really big dictionary. Imagine your dictionary has a million words and you are looking for a 4 letter word. The number of permutations you get are 24, so it will need to search through the dictionary 24 times. If the average word is found around the middle, that's 12 million comparisons. Yuck! Read up on binary search algorithms if this is an issue for you.
Although, if the dictionary is that large, there is also the problem that you need to send the entire thing to the client. You could reduce that a bit using a zip algorithm of some sort, but even then, it might just be too much. At that point, you'll probably want to use a server-side anagram checker and send back the results.
One other thing to note is that you should build your list of anagrams into an array, throw out any bad ones, and then make sure to remove any duplicates. You'll notice with the code I provided that "baaann" comes up with a ton of duplicates of "banana". That's because your algorithm to create permutations doesn't take into account duplicate letters.
来源:https://stackoverflow.com/questions/24085190/make-anagrams-generator-in-javascript-finding-only-words-contained-in-a-list