TungstenVn

CodeLearn B

Oct 28th, 2019
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var outtemp = ""; var outtemp2 = [];
  2. //longestWordChain(["java", "python", "angualar", "r", "ruby"], "angualar");
  3. function longestWordChain(keywords, startWord){
  4.     var check = false;
  5.     var chuoihientai = startWord;
  6.     for(var i = 0;i < keywords.length;i++){
  7.         if(startWord[startWord.length -1] == keywords[i][0]  && chuoihientai.split("-").indexOf(keywords[i]) == -1){        
  8.             dequytimchu(chuoihientai + "-" + keywords[i],keywords[i],keywords);        
  9.             check = true;
  10.         }
  11.     }
  12.     if(!check){
  13.         return startWord;
  14.     }
  15.     if(outtemp2.length == 1){
  16.       return outtemp2[0];
  17.     }
  18.     var checklength = "";
  19.     for(var i = 0;i<outtemp2.length-1;i++){
  20.       if(outtemp2[i].split("-").length > outtemp2[i+1].split("-").length){
  21.         checklength =  outtemp2[i];  
  22.       }else{
  23.         checklength =  outtemp2[i+1];  
  24.       }
  25.     }
  26.     var c = 0;
  27.     for(var i = 0;i<outtemp2.length;i++){
  28.       if(checklength.split("-").length == outtemp2[i].split("-").length && checklength != outtemp2[i+1]){
  29.         c++;
  30.       }
  31.     }
  32.    
  33.     if(c == 1){
  34.       return checklength;    
  35.     }
  36.     var outtemp3 = [];var checkdodai = 0;
  37.     for(var i = 0;i<outtemp2.length;i++){
  38.       if(outtemp2[i].split("-").length >= checkdodai){
  39.         checkdodai = outtemp2[i].split("-").length;
  40.       }
  41.     }
  42.      for(var i = 0;i<outtemp2.length;i++){
  43.       if(outtemp2[i].split("-").length == checkdodai){
  44.         outtemp3.push(outtemp2[i]);
  45.       }
  46.     }
  47.     outtemp3.sort();  
  48.     return outtemp3;
  49. }
  50. function dequytimchu(chuoihientai,tutieptheo,keywords){
  51.     var check = false;
  52.     for(var i = 0;i < keywords.length;i++){
  53.        
  54.         if(tutieptheo[tutieptheo.length -1] == keywords[i][0] && keywords[i] != tutieptheo && chuoihientai.split("-").indexOf(keywords[i]) == -1){  
  55.             dequytimchu(chuoihientai +"-"+keywords[i],keywords[i],keywords);        
  56.         }
  57.     }  
  58.     if(outtemp.length <= chuoihientai.length){
  59.             outtemp = chuoihientai;
  60.             outtemp2.push(chuoihientai);
  61.     }
  62. }
Add Comment
Please, Sign In to add comment