Not sure about the problem that you're facing, this'll pass though:
class Solution:
def uniqueMorseRepresentations(self, words):
morse_map = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
return len({''.join(morse_map[ord(char) - 97] for char in word) for word in words})
97 is ord('a'):
class Solution:
def uniqueMorseRepresentations(self, words):
morse_map = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
return len({''.join(morse_map[ord(char) - ord('a')] for char in word) for word in words})
I don't see a return statement or a set() in your solution. There are two simple steps:
- Add visited transformations into a set
- Return the length of the set
Here is also a Java version using a HashSet (similar to the set() in Python), if you'd be interested:
public final class Solution {
public static final int uniqueMorseRepresentations(
final String[] words
) {
final String[] morseMap = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
Set<String> transformations = new HashSet<>();
for (String word : words) {
StringBuilder transformation = new StringBuilder();
for (int index = 0; index < word.length(); index++)
transformation.append(morseMap[word.charAt(index) - 97]);
transformations.add(transformation.toString());
}
return transformations.size();
}
}
References
- For additional details, please see the Discussion Board where you can find plenty of well-explained accepted solutions with a variety of languages including low-complexity algorithms and asymptotic runtime/memory analysis1, 2.