Unique Morse Code Words
class Solution {
public int uniqueMorseRepresentations(String[] words) {
String [] codes = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
HashSet output = new HashSet();
for ( int i = 0 ; i < words.length ; i++){
StringBuilder transformation = new StringBuilder();
for ( int j = 0 ; j <span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>< words[i].length() ; j++ ){
transformation.append(codes[words[i].charAt(j)-97]);
}
output.add(transformation.toString());
}
return output.size();
}
}
Like this:
Like Loading...
Related