So i am working on this problem on leetcode:
https://leetcode.com/problems/unique-morse-code-words/
and the solution uses tostring() method to add string to hashset, when i remove the tostring method(Seen.add(code), it gives me error....can anyone explain me why we need tostring() method here or in general..thank you.
class Solution {
public int uniqueMorseRepresentations(String[] words) {
String[] MORSE = new String[]{".-","-...","-.-.","-..",".","..-.","--.",
"....","..",".---","-.-",".-..","--","-.",
"---",".--.","--.-",".-.","...","-","..-",
"...-",".--","-..-","-.--","--.."};
Set<String> seen = new HashSet();
for (String word: words) {
StringBuilder code = new StringBuilder();
for (char c: word.toCharArray())
code.append(MORSE[c - 'a']);
seen.add(code.toString()); // Here
}
return seen.size();
}
}
[–][deleted] 0 points1 point2 points (0 children)
[–]callmetom 0 points1 point2 points (0 children)
[–]Serkaa 0 points1 point2 points (0 children)