Skip to content

Commit 3107376

Browse files
add 824
1 parent 5a41238 commit 3107376

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|824|[Goat Latin](https://leetcode.com/problems/goat-latin/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_824.java) | O(n) | O(1) | |Easy|
2526
|821|[Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_821.java) | O(n) | O(k) (k is the number of char C in S) | |Easy|
2627
|819|[Most Common Word](https://leetcode.com/problems/most-common-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_819.java) | O(m+n) | O(n) | |Easy| HashMap
2728
|811|[Subdomain Visit Count](https://leetcode.com/problems/subdomain-visit-count/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_811.java) | O(n) | O(n) | |Easy| HashMap
@@ -194,7 +195,7 @@ Your ideas/fixes/algorithms are more than welcome!
194195
|547|[Friend Circles](https://leetcode.com/problems/friend-circles/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_547.java) | O(n^2) |O(n) | |Medium | Union Find
195196
|546|[Remove Boxes](https://leetcode.com/problems/remove-boxes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_546.java) | O(n^3) |O(n^3) | |Hard| DFS, DP
196197
|545|[Boundary of Binary Tree](https://leetcode.com/problems/boundary-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_545.java) | O(n) |O(n) | |Medium | Recursion
197-
|544|[Output Contest Matches](https://leetcode.com/problems/output-contest-matches/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_544.java) | O(n) |O(n) | |Medium | Recursion
198+
|544|[Output Contest Matches](https://leetcode.com/problems/output-a824-matches/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_544.java) | O(n) |O(n) | |Medium | Recursion
198199
|543|[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_543.java) | O(n) |O(h) || Easy | Tree/DFS/Recursion
199200
|542|[01 Matrix](https://leetcode.com/problems/01-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_542.java) | O(m*n) |O(n) | |Medium | BFS
200201
|541|[Reverse String II](https://leetcode.com/problems/reverse-string-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_541.java) | O(n) |O(1) | |Easy | String
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
/**
8+
* 824. Goat Latin
9+
*
10+
* A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only.
11+
*
12+
* We would like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.)
13+
*
14+
* The rules of Goat Latin are as follows:
15+
*
16+
* If a word begins with a vowel (a, e, i, o, or u), append "ma" to the end of the word.
17+
* For example, the word 'apple' becomes 'applema'.
18+
*
19+
* If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add "ma".
20+
* For example, the word "goat" becomes "oatgma".
21+
*
22+
* Add one letter 'a' to the end of each word per its word index in the sentence, starting with 1.
23+
* For example, the first word gets "a" added to the end, the second word gets "aa" added to the end and so on.
24+
*
25+
* Return the final sentence representing the conversion from S to Goat Latin.
26+
*
27+
* Example 1:
28+
*
29+
* Input: "I speak Goat Latin"
30+
* Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
31+
*
32+
* Example 2:
33+
*
34+
* Input: "The quick brown fox jumped over the lazy dog"
35+
* Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
36+
*
37+
* Notes:
38+
*
39+
* S contains only uppercase, lowercase and spaces. Exactly one space between each word.
40+
* 1 <= S.length <= 100.
41+
*/
42+
public class _824 {
43+
44+
public static class Solution1 {
45+
public String toGoatLatin(String S) {
46+
StringBuilder sb = new StringBuilder();
47+
Set<Character> vowels =
48+
new HashSet(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
49+
String[] words = S.split(" ");
50+
for (int i = 0; i < words.length; i++) {
51+
if (vowels.contains(words[i].charAt(0))) {
52+
String newWord = words[i] + "ma";
53+
int j = i + 1;
54+
while (j-- > 0) {
55+
newWord += 'a';
56+
}
57+
sb.append(newWord);
58+
sb.append(" ");
59+
} else {
60+
StringBuilder subSb = new StringBuilder(words[i].substring(1));
61+
subSb.append(words[i].charAt(0));
62+
subSb.append("ma");
63+
int j = i + 1;
64+
while (j-- > 0) {
65+
subSb.append("a");
66+
}
67+
sb.append(subSb.toString());
68+
sb.append(" ");
69+
}
70+
}
71+
return sb.substring(0, sb.length() - 1);
72+
}
73+
}
74+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._824;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _824Test {
10+
private static _824.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _824.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("Imaa peaksmaaa oatGmaaaa atinLmaaaaa",
20+
solution1.toGoatLatin("I speak Goat Latin"));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
assertEquals(
26+
"heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa",
27+
solution1.toGoatLatin("The quick brown fox jumped over the lazy dog"));
28+
}
29+
}

0 commit comments

Comments
 (0)