Unique Morse Code Words Solutions in C++
Number 804
Difficulty Easy
Acceptance 77.0%
Link LeetCode
Other languages —
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/unique-morse-code-words/description/// Author : Hao Chen// Date : 2018-06-29class Solution {public:int uniqueMorseRepresentations(vector<string>& words) {string MorseTable[26] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};unordered_map<string, bool> transformations;for (auto word : words) {string morse;for (auto ch : word) {morse += MorseTable[ ch - 'a' ];}transformations[morse]=true;}return transformations.size();}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/unique-morse-code-words/description//// Author : liuyubobobo/// Time : 2018-03-24#include <iostream>#include <vector>#include <string>#include <unordered_set>using namespace std;/// Using Hash Set/// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {public:int uniqueMorseRepresentations(vector<string>& words) {vector<string> morse = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};unordered_set<string> bag;for(const string& word: words){string code = "";for(char c: word)code += morse[c - 'a'];bag.insert(code);}return bag.size();}};int main() {vector<string> words = {"gin", "zen", "gig", "msg"};cout << Solution().uniqueMorseRepresentations(words) << endl;return 0;}