Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var outtemp = ""; var outtemp2 = [];
- //longestWordChain(["java", "python", "angualar", "r", "ruby"], "angualar");
- function longestWordChain(keywords, startWord){
- var check = false;
- var chuoihientai = startWord;
- for(var i = 0;i < keywords.length;i++){
- if(startWord[startWord.length -1] == keywords[i][0] && chuoihientai.split("-").indexOf(keywords[i]) == -1){
- dequytimchu(chuoihientai + "-" + keywords[i],keywords[i],keywords);
- check = true;
- }
- }
- if(!check){
- return startWord;
- }
- if(outtemp2.length == 1){
- return outtemp2[0];
- }
- var checklength = "";
- for(var i = 0;i<outtemp2.length-1;i++){
- if(outtemp2[i].split("-").length > outtemp2[i+1].split("-").length){
- checklength = outtemp2[i];
- }else{
- checklength = outtemp2[i+1];
- }
- }
- var c = 0;
- for(var i = 0;i<outtemp2.length;i++){
- if(checklength.split("-").length == outtemp2[i].split("-").length && checklength != outtemp2[i+1]){
- c++;
- }
- }
- if(c == 1){
- return checklength;
- }
- var outtemp3 = [];var checkdodai = 0;
- for(var i = 0;i<outtemp2.length;i++){
- if(outtemp2[i].split("-").length >= checkdodai){
- checkdodai = outtemp2[i].split("-").length;
- }
- }
- for(var i = 0;i<outtemp2.length;i++){
- if(outtemp2[i].split("-").length == checkdodai){
- outtemp3.push(outtemp2[i]);
- }
- }
- outtemp3.sort();
- return outtemp3;
- }
- function dequytimchu(chuoihientai,tutieptheo,keywords){
- var check = false;
- for(var i = 0;i < keywords.length;i++){
- if(tutieptheo[tutieptheo.length -1] == keywords[i][0] && keywords[i] != tutieptheo && chuoihientai.split("-").indexOf(keywords[i]) == -1){
- dequytimchu(chuoihientai +"-"+keywords[i],keywords[i],keywords);
- }
- }
- if(outtemp.length <= chuoihientai.length){
- outtemp = chuoihientai;
- outtemp2.push(chuoihientai);
- }
- }
Add Comment
Please, Sign In to add comment