解法一
用StringBuilder来进行翻译连接,用Set完成翻译结果的去重。
import java.util.HashSet;import java.util.Set;class Solution {private final String[] CODE = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};public int uniqueMorseRepresentations(String[] words) {String[] morse = new String[words.length];Set<String> stringSet = new HashSet<>();int i, j;for (i = 0; i < words.length; ++i) {StringBuilder str = new StringBuilder();for (j = 0; j < words[i].length(); ++j) {str.append(CODE[words[i].charAt(j) - 97]);}System.out.println(str);stringSet.add(str.toString());}return stringSet.size();}}
