classSolution: defuniqueMorseRepresentations(self, words: List[str]) -> int: returnlen(set("".join(MORSE[ord(ch) - ord('a')] for ch in word) for word in words))
publicintUniqueMorseRepresentations(string[] words) { ISet<string> seen = new HashSet<string>(); foreach (string word in words) { StringBuilder code = new StringBuilder(); foreach (char c in word) { code.Append(MORSE[c - 'a']); } seen.Add(code.ToString()); } return seen.Count; } }
intuniqueMorseRepresentations(char ** words, int wordsSize){ HashItem * seen = NULL; for (int i = 0; i < wordsSize; i++) { HashItem * pEntry = NULL; int len = strlen(words[i]); int pos = 0; char code[MAX_STR_LEN]; for (int j = 0; j < len; j++) { pos += sprintf(code + pos, "%s", MORSE[words[i][j] - 'a']); } HASH_FIND_STR(seen, code, pEntry); if (NULL == pEntry) { pEntry = (HashItem *)malloc(sizeof(HashItem)); strcpy(pEntry->key, code); HASH_ADD_STR(seen, key, pEntry); } } int ans = HASH_COUNT(seen); HashItem * curr = NULL, * tmp = NULL; HASH_ITER(hh, seen, curr, tmp) { HASH_DEL(seen, curr); free(curr); } return ans; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12
constMORSE = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]; var uniqueMorseRepresentations = function(words) { const seen = newSet(); for (const word of words) { let code = ''; for (const ch of word) { code += (MORSE[ch.charCodeAt() - 'a'.charCodeAt()]); } seen.add(code); } return seen.size; }
funcuniqueMorseRepresentations(words []string)int { set := map[string]struct{}{} for _, word := range words { trans := &strings.Builder{} for _, ch := range word { trans.WriteString(morse[ch-'a']) } set[trans.String()] = struct{}{} } returnlen(set) }