Skip to content

Instantly share code, notes, and snippets.

@bigomega
Created April 16, 2016 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigomega/8164e9da5bf03b6916f2040e05bc8dd5 to your computer and use it in GitHub Desktop.
Save bigomega/8164e9da5bf03b6916f2040e05bc8dd5 to your computer and use it in GitHub Desktop.
Coding problem
dict = ['cat','cot','cog','bog','bat','bap','map', 'mat'];
start = 'cat';
end = 'map';
isLinked = function(w1, w2){
var flag = false;
for (var i = 0; i < w1.length; i++) {
if(w1[i] != w2[i]){
if(flag)
return false;
flag = true;
}
}
return true;
}
myFunc = function(status, dict_copy) {
var out = [];
for (var i = dict_copy.length - 1; i >= 0; i--) {
var dictCopy = dict_copy.slice(0);
var st = status.slice(0);
var next = dictCopy[i];
dictCopy.splice(i, 1);
if(isLinked(st[st.length - 1], next)){
st.push(next);
if(next == end){
out.push(st);
} else {
var zzz = myFunc(st, dictCopy, out);
out = out.concat(zzz);
}
}
}
return out;
}
if(start == end){
x = [start];
} else {
var dictCopy = dict.filter(function(val){ return val != start;});
x = myFunc([start], dictCopy);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment