Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Question:
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, "cab" 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.
For example:

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

Answer I did:
I am getting wrong for the input when I submitted my code of the above example
I am getting output as 4.
But when I debug by taking the above one in onlinegdb.com , I am getting correct output i.e 2

What I have tried:

C++
#include<string.h>

int uniqueMorseRepresentations(char ** words, int wordsSize){
    int i=0,j,count=wordsSize;
    for(;i<wordsSize-1;i++)
    {
        if(words[i]!=NULL)
        {
             for(j=i+1;j<wordsSize;j++)
        {
            if(words[j]!=NULL)
            {
               if(strcpy(words[i],words[j])==0)
               {
                   count--;
               }
            }
        }
            words[i]=NULL;
        }
    }
    return count;
}
Posted
Updated 9-Jul-21 22:51pm
v2
Comments
Richard MacCutchan 10-Jul-21 4:54am    
Where do you check for unique transformation strings?
_-_-_-me 10-Jul-21 7:41am    
I checked it with the value of count .
Is it incorrect?
Richard MacCutchan 10-Jul-21 8:03am    
I cannot quite figure out what that code is supposed to be doing. But thinking logically, you should be building an array of the transformed words. Once you have that then you should use strcmp<code> to find the number of differences.
_-_-_-me 10-Jul-21 8:53am    
I mistakenly used strcpy() instead of strcmp() .
I used strcmp() now but even then I am not getting correct output.
Richard MacCutchan 11-Jul-21 4:02am    
Well you need to show your code and explain what is happening.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900