Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class TestBucketMain {
- public static int uniqueMorseRepresentations(String[] words) {
- String [] A = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
- "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",
- ".--","-..-","-.--","--.."};
- String [] B = new String[words.length];
- for(int i = 0; i < words.length; i++){
- String selected = words[i];
- for (int j= 0; j < selected.length(); j++) {
- char m = selected.charAt(j);
- int num = (int)m-97;
- String transform = A[num];
- if (B[i] == null) {
- B[i] = transform;
- }
- else {
- B[i] = B[i] + transform;
- }
- }
- }
- Arrays.sort(B);
- int count = 1;
- if (B[0] != null) {
- String check = B[0];
- for (int i = 1; i < B.length; i++) {
- if (!check.equals(B[i])) {
- count++;
- check=B[i];
- }
- }
- }
- return count;
- }
- public static void main(String[] args) {
- String [] words = {"gin", "zen", "gig", "msg", "carrot", "platypus"};
- int c = uniqueMorseRepresentations(words);
- System.out.println("unique words " + c);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement