Skip to content

Commit 2369fcc

Browse files
add 804
1 parent 39c864d commit 2369fcc

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Your ideas/fixes/algorithms are more than welcome!
2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
2525
|806|[Number of Lines To Write String](https://leetcode.com/problems/number-of-lines-to-write-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_806.java) | O(n) | O(1) | |Easy|
26+
|804|[Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_804.java) | O(S) | O(S) | |Easy|
2627
|799|[Champagne Tower](https://leetcode.com/problems/champagne-tower/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_799.java) | O(r^2) or O(1) | O(r^2) or O(1) | |Medium|
2728
|796|[Rotate String](https://leetcode.com/problems/rotate-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_796.java) | O(n) | O(1) | |Easy|
2829
|791|[Custom Sort String](https://leetcode.com/problems/custom-sort-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_791.java) | O(n+m) | O(1) | |Medium|
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* 804. Unique Morse Code Words
8+
9+
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.
10+
11+
For convenience, the full table for the 26 letters of the English alphabet is given below:
12+
13+
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
14+
15+
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.
16+
17+
Return the number of different transformations among all words we have.
18+
19+
Example:
20+
Input: words = ["gin", "zen", "gig", "msg"]
21+
Output: 2
22+
Explanation:
23+
The transformation of each word is:
24+
"gin" -> "--...-."
25+
"zen" -> "--...-."
26+
"gig" -> "--...--."
27+
"msg" -> "--...--."
28+
29+
There are 2 different transformations, "--...-." and "--...--.".
30+
31+
Note:
32+
The length of words will be at most 100.
33+
Each words[i] will have length in range [1, 12].
34+
words[i] will only consist of lowercase letters.
35+
*/
36+
public class _804 {
37+
public static class Solution1 {
38+
public int uniqueMorseRepresentations(String[] words) {
39+
String[] morseCodes =
40+
new String[] {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
41+
"-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-",
42+
".--", "-..-", "-.--", "--.."};
43+
Set<String> concatenation = new HashSet<>();
44+
StringBuilder sb = new StringBuilder();
45+
for (String word : words) {
46+
sb.setLength(0);
47+
for (char c : word.toCharArray()) {
48+
sb.append(morseCodes[c - 'a']);
49+
}
50+
concatenation.add(sb.toString());
51+
}
52+
return concatenation.size();
53+
}
54+
}
55+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._804;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _804Test {
10+
private static _804.Solution1 solution1;
11+
private static String[] words;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _804.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
words = new String[] {"gin", "zen", "gig", "msg"};
21+
assertEquals(2, solution1.uniqueMorseRepresentations(words));
22+
}
23+
}

0 commit comments

Comments
 (0)