image_2021-10-16_17-04-24.png
60.9 KB
#N804 Unique Morse Code Words
problem link=>https://leetcode.com/problems/unique-morse-code-words/
#solution
problem link=>https://leetcode.com/problems/unique-morse-code-words/
#solution
class Solution {
public int uniqueMorseRepresentations(String[] words) {
String[] morseAlphabet={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
String morse="";
for(int i=0; i<words.length; i++){
morse="";
for(char ch:words[i].toCharArray()){
morse+=morseAlphabet[ch-'a'];
}
words[i]=morse;
}
Arrays.sort(words);
int count=words.length;
for(int i=0; i<words.length-1; i++){
if(words[i].equals(words[i+1]))
count--;
}
return count;
}
}