UniqueMorseCodeWords [source code]


public class UniqueMorseCodeWords {
static
/******************************************************************************/
class Solution {
    static String[] codes = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};

    public int uniqueMorseRepresentations(String[] words) {
        Set<String> encoded = new HashSet<> ();
        Set<String> seen = new HashSet<> ();
        for (String word : words) {
            if (seen.contains (word))
                continue;
            seen.add (word);
            encoded.add (encode (word));
        }
        return encoded.size ();
    }

    String encode (String s) {
        StringBuilder sb = new StringBuilder ();
        for (char c : s.toCharArray ()) {
            sb.append (codes[c - 'a']);
        }
        return sb.toString ();
    }
}
/******************************************************************************/

    public static void main(String[] args) {
        UniqueMorseCodeWords.Solution tester = new UniqueMorseCodeWords.Solution();
        String[][] inputs = {
            {"gin", "zen", "gig", "msg"}, {"2"},
        };
        for (int i = 0; i < inputs.length / 2; i++) {
            System.out.println (Printer.separator ());
            String[] words = inputs[2 * i];
            int ans = Integer.parseInt (inputs[2 * i + 1][0]), output = tester.uniqueMorseRepresentations (words);
            System.out.printf ("%s\n%s, expected: %d\n", 
                Printer.array (words), Printer.wrap (output + "", output == ans ? 92 : 91), ans
            );
        }
    }
}

没什么好办法, 直接就encode, 然后扔到set里count算了; 因为是easy题, 所以估计没有什么滑头?

UNFINISHED


uwi:

class Solution {  
    String[] T = {  
            ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."  
    };  
    public int uniqueMorseRepresentations(String[] words) {  
        Set<String> set = new HashSet<>();  
        for(String w : words){  
            StringBuilder sb = new StringBuilder();  
            for(int i = 0;i < w.length();i++){  
                sb.append(T[w.charAt(i)-'a']);  
            }  
            set.add(sb.toString());  
        }  
        return set.size();  
    }  
}

Problem Description

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.

For convenience, the full table for the 26 letters of the English alphabet is given below:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cab" can be written as "-.-.-....-", (which is the concatenation "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word.

Return the number of different transformations among all words we have.

Example:

Input: words = ["gin", "zen", "gig", "msg"]  
Output: 2  
Explanation:   
The transformation of each word is:  
"gin" -> "--...-."  
"zen" -> "--...-."  
"gig" -> "--...--."  
"msg" -> "--...--."  

There are 2 different transformations, "--...-." and "--...--.".

Note:

  • The length of words will be at most 100.
  • Each words[i] will have length in range [1, 12].
  • words[i] will only consist of lowercase letters.

Difficulty:Easy
Total Accepted:10.1K
Total Submissions:13.4K
Contributor:awice

results matching ""

    No results matching ""