commit 4c08e5fc6018404c2b5fd2225b1a146fcc7ce89c parent d2a8aa6e7567513e16896914d9129e70cd3f1f5c Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Fri, 28 Apr 2023 13:14:19 -0500 Completed similar string groups problem with JS Diffstat:
| A | similar-string-groups/similar-string-groups.js | | | 32 | ++++++++++++++++++++++++++++++++ |
1 file changed, 32 insertions(+), 0 deletions(-)
diff --git a/similar-string-groups/similar-string-groups.js b/similar-string-groups/similar-string-groups.js @@ -0,0 +1,32 @@ +//Find the number of similar elements in a list where +//the similar items have only one character out of order +/** + * @param {string[]} strs + * @return {number} + */ +var numSimilarGroups = function(strs) { + let similar = 0; + + for(let i = 0 ; i < strs.length; ++i){ + for(let x = i + 1; x < strs.length ; ++x){ + if(compare_words(strs[i] , strs[x])){ + similar += 1; + } + } + } + + return similar; +}; + +function compare_words(str1 , str2){ + let different_vals = 0; + for(let i = 0 ; i < str1.length ; ++i){ + if(str1[i] != str2[i]){ + different_vals += 1; + } + if(different_vals > 2){ + return false; + } + } + return true; +}