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