similar-string-groups.js (738B)
1 //Find the number of similar elements in a list where 2 //the similar items have only one character out of order 3 /** 4 * @param {string[]} strs 5 * @return {number} 6 */ 7 var numSimilarGroups = function(strs) { 8 let similar = 0; 9 10 for(let i = 0 ; i < strs.length; ++i){ 11 for(let x = i + 1; x < strs.length ; ++x){ 12 if(compare_words(strs[i] , strs[x])){ 13 similar += 1; 14 } 15 } 16 } 17 18 return similar; 19 }; 20 21 function compare_words(str1 , str2){ 22 let different_vals = 0; 23 for(let i = 0 ; i < str1.length ; ++i){ 24 if(str1[i] != str2[i]){ 25 different_vals += 1; 26 } 27 if(different_vals > 2){ 28 return false; 29 } 30 } 31 return true; 32 }