문제 링크 : Count Pairs Of Similar Strings
/**
* @param {string[]} words
* @return {number}
*/
var similarPairs = function(words) {
let result = 0
for(let i = 0; i<words.length; i++) {
for(let j = i+1; j<words.length; j++) {
const firstWord = [...new Set(words[i])].sort().join('')
const secondWord = [...new Set(words[j])].sort().join('')
if(firstWord === secondWord) result ++
}
}
return result
};
/**
* @param {string[]} words
* @return {number}
*/
var similarPairs = function(words) {
const newWords = []
let count = 0
// use hashSet to filter the duplicate words and sort the word.
for (let i = 0; i < words.length; i++) {
newWords[i] = [...new Set(words[i])].sort().join('')
}
// find if the word are the same for current and next.
for (let i = 0; i < newWords.length - 1; i++) {
for (let j = i+1; j < newWords.length; j++) {
if (newWords[i] === newWords[j]) {
count += 1
}
}
}
return count
};