Advertisement
Guest User

804. Unique Morse Code Words

a guest
Apr 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class TestBucketMain {
  4.  
  5.     public static int uniqueMorseRepresentations(String[] words) {
  6.        
  7.         String [] A = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
  8.                 "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",
  9.                 ".--","-..-","-.--","--.."};           
  10.         String [] B = new String[words.length];    
  11.        
  12.         for(int i = 0; i < words.length; i++){
  13.             String selected = words[i];
  14.            
  15.             for (int j= 0; j < selected.length(); j++) {                       
  16.                 char m = selected.charAt(j);
  17.                 int num = (int)m-97;
  18.                 String transform = A[num]; 
  19.                
  20.                 if (B[i] == null) {
  21.                     B[i] = transform;
  22.                 }                  
  23.                 else {
  24.                 B[i] = B[i] + transform;
  25.                 }
  26.             }
  27.         }              
  28.         Arrays.sort(B);
  29.         int count = 1;
  30.         if (B[0] != null) {
  31.             String check = B[0];
  32.             for (int i = 1; i < B.length; i++) {
  33.                 if (!check.equals(B[i])) {
  34.                     count++;
  35.                     check=B[i];
  36.                 }              
  37.             }
  38.         }
  39.       return count;                            
  40.     }
  41.        
  42.     public static void main(String[] args) {
  43.  
  44.         String [] words = {"gin", "zen", "gig", "msg", "carrot", "platypus"};
  45.         int c = uniqueMorseRepresentations(words);
  46.         System.out.println("unique words " + c);
  47.    
  48.     }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement