[LeetCode] 804. Unique Morse Code Words

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-""b" maps to "-...""c" maps to "-.-.", and so on.

For convenience, the full table for the 26 letters of the English alphabet is given below:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cba" can be written as "-.-..--...", (which is the concatenation "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word.

Return the number of different transformations among all words we have.

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 "--...--.".

Note:

  • The length of words will be at most 100.
  • Each words[i] will have length in range [1, 12].
  • words[i] will only consist of lowercase letters.

Subject to the effect:

Each letter corresponds to a Morse code, a string can be converted to the corresponding sequence consisting of Morse code. Different strings may also have the same Morse code sequence. Title given several strings, required to obtain the type of Morse code sequence corresponding to.

 

method:

Requirements demand different strings generated, and each letter corresponds to a Morse code, think about the map.

The Morse code string generated sequence into the result of the map, the final results were calculated as the size of the map is the number of kinds of Morse code.

code show as below:

class Solution {
public:
    int uniqueMorseRepresentations(vector<string>& words) {
        map<char,string> wordsMap={{'a',".-"},{'b',"-..."},{'c',"-.-."},{'d',"-.."},{'e',"."},{'f',"..-."},{'g',"--."},{'h',"...."},{'i',".."},{'j',".---"},{'k',"-.-"},{'l',".-.."},{'m',"--"},{'n',"-."},{'o',"---"},{'p',".--."},{'q',"--.-"},{'r',".-."},{'s',"..."},{'t',"-"},{'u',"..-"},{'v',"...-"},{'w',".--"},{'x',"-..-"},{'y',"-.--"},{'z',"--.."}};
        
        if (words.empty())return 0;
        map<string, int> res;
        for (int i = 0; i < words.size(); ++i) {
            string temp = "";
            for (int j = 0; j < words[i].size(); ++j) {
                temp.insert(temp.size(), wordsMap[words[i][j]]);
            }
            res.insert({ temp, 1 });
        }
        return res.size();
    }
};

*note:

1. To insert a character string using the insert () function, the first parameter indicates the placement position of the character, type int, the second parameter is inserted character.

2. The insert element used to map insert () function, the element type is a key element of the type of the map, so to add {} key-value pair comprising up.

Guess you like

Origin www.cnblogs.com/cff2121/p/11236945.html