Facebook
From Hussain Imtiaz, 4 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 273
  1. class Solution {
  2. public:
  3.     vector<string> characters = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
  4.     string getMorse(string &word) {
  5.         int index = 0;
  6.         string result;
  7.         for (int i = 0; i < word.size(); i++) {
  8.             index = word[i] - 'a';
  9.             result += characters[index];
  10.         }
  11.         return result;
  12.     }
  13.     int uniqueMorseRepresentations(vector<string>& words) {
  14.         int count = 0;
  15.         string morse;
  16.         unordered_map<string, string> trans;
  17.         for (int i = 0; i < words.size(); i++) {
  18.             morse = getMorse(words[i]);
  19.             if (trans.find(morse) == trans.end()) {
  20.                 trans[morse] = words[i];
  21.                 count++;
  22.             }
  23.         }
  24.         return count;
  25.     }
  26. };