Example:
Input: words = \["gin", "zen", "gig", "msg"\]
Output: 2
Explanation:
The transformation of each word is:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."
There are 2 different transformations, "--...-." and "--...--.".
/** * @param {string[]} words * @return {number} */ var uniqueMorseRepresentations = function(words) { var morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]; var count = 0; var morsewords = []; //遍历words for(let word of words){ var morseword = ''; var a = 'a'; var flag = true; for(let c of word){ morseword += morse[c.charCodeAt(0)-a.charCodeAt(0)]; //每个word转换为morseword } for(let m of morsewords){ if(morseword == m){ flag = false; break; }else { continue } } if(flag){ count +=1; } morsewords.push(morseword); } return count; };