-4

Working on a leetcode exercise, which you can see here: https://leetcode.com/problems/unique-morse-code-words/

I'm having trouble getting the right answer, but even more trouble finding the problem. I'm trying to use cout to print the vectors I'm working with to see what's wrong but it seems to cout empty string for some reason.

Here's my code...

#include <array>
#include <algorithm>
#include <vector>
#include <iostream>

using namespace std;

class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words) {
    int num_of_uniq_words = 0;
    string arr[] = {"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"}; 
    string maps[] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..",
                     "--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
    vector<string> all_words_morse;
    for (int i = 0; i < words.size(); i++) {
        string morse;
        for (int j = 0; j < words[i].length(); j++){

                for(int q = 0; q < sizeof(arr)/sizeof(arr[0]); q++) {
                    if (arr[q] == to_string(words[i].at(j))) 
                        morse.append(maps[q]);
                }


        }
        //cout << morse << endl;
        all_words_morse.push_back(morse);
    }
    vector<string> uniq_words;
    for(int i = 0; i < all_words_morse.size(); i++) {
        if (find(uniq_words.begin(), uniq_words.end(), all_words_morse[i]) == uniq_words.end()) //not present
            uniq_words.push_back(all_words_morse[i]);
    }
    //printing
    for (int i = 0; i < all_words_morse.size(); i++)
        cout << all_words_morse[i] << " ";
    cout << "\n" << endl;
    for (int i = 0; i < uniq_words.size(); i++)
        cout << uniq_words[i] << " ";
    cout << "\n" << endl;

    num_of_uniq_words = uniq_words.size();
    return num_of_uniq_words;
}
};

and with the test case input of ["gin", "zen", "gig", "msg"] the sdtout is ... "

"
which is about 4 lines of empty string and I don't understand why. Does anyone have any advice or know what I'm doing wrong?? Thanks

alex Brock
  • 23
  • 6
  • 7
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver May 13 '19 at 15:30
  • 3
    Hint: `std::map`. Associate a character with a Morse code string. – Thomas Matthews May 13 '19 at 15:42
  • I suspect that [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) will be useful reading. – Jesper Juhl May 13 '19 at 15:45
  • Honestly, if you want to represent single characters, use `char`, not `std::string`... Agreeqing with @ThomasMatthews, but you might consider `std::unordered_map` as alternative. Should be faster than `std::map`... – Aconcagua May 13 '19 at 15:49
  • 3
    Another idea: Array of strings. Use the character as an index. For example, `translate['a'] = ".-";`. Much faster than an `std::unordered_map`. Since the quantity of elements is known at compile time, an array can be used. – Thomas Matthews May 13 '19 at 15:52

2 Answers2

0

Problem

The function std::to_string doesn't take a char as parameter so it implicitly converts it to an int, which return a string of the number. For exemple: to_string('a') -> to_string(97) -> "97".

Fix

Properly convert your char to a string using std::string(1,words[i].at(j)) or use the substr method of std::string.

Alternative approach

As suggested by Thomas Matthews in the comments, use a std::map<char, std::string> and create a map to avoid having to manage two lists, the conversion, and save time using a structure that performs lookup in O(logn) instead of O(n).

0

The problem is usage of to_string function. It does not do what you think it does:

std::to_string

Converts a numeric value to std::string.

And you pass a char to it, so it interpretes char as a numeric value, that is its ASCII value. And for 'g' you get '103' for example.

And why is arr an array of string rather than chars, if it only contains characters? If it was an array of chars, you wouldn't need to_string function in the first place.

P.S. To find problems in your code, best thing is to debug it. A debugger is a tool every programmer should use. So please check it out.

Community
  • 1
  • 1
Nellie Danielyan
  • 1,001
  • 7
  • 19