Skip to content

Commit 59ca0e5

Browse files
committed
Add solution #824
1 parent 261d779 commit 59ca0e5

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
791|[Custom Sort String](./0791-custom-sort-string.js)|Medium|
2929
804|[Unique Morse Code Words](./0804-unique-morse-code-words.js)|Easy|
3030
819|[Most Common Word](./0819-most-common-word.js)|Easy|
31+
824|[Goat Latin](./0824-goat-latin.js)|Easy|
3132
890|[Find and Replace Pattern](./0890-find-and-replace-pattern.js)|Medium|
3233
916|[Word Subsets](./0916-word-subsets.js)|Medium|
3334
929|[Unique Email Addresses](./0929-unique-email-addresses.js)|Easy|

solutions/0824-goat-latin.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 824. Goat Latin
3+
* https://leetcode.com/problems/goat-latin/
4+
* Difficulty: Easy
5+
*
6+
* A sentence S is given, composed of words separated by spaces.
7+
* Each word consists of lowercase and uppercase letters only.
8+
*
9+
* We would like to convert the sentence to "Goat Latin"
10+
* (a made-up language similar to Pig Latin.)
11+
*
12+
* The rules of Goat Latin are as follows:
13+
* - If a word begins with a vowel (a, e, i, o, or u), append "ma" to the
14+
* end of the word. For example, the word 'apple' becomes 'applema'.
15+
* - If a word begins with a consonant (i.e. not a vowel), remove the
16+
* first letter and append it to the end, then add "ma".
17+
* For example, the word "goat" becomes "oatgma".
18+
* - Add one letter 'a' to the end of each word per its word index in
19+
* the sentence, starting with 1.
20+
* For example, the first word gets "a" added to the end, the second word gets
21+
* "aa" added to the end and so on.
22+
*
23+
* Return the final sentence representing the conversion from S to Goat Latin.
24+
*/
25+
26+
/**
27+
* @param {string} S
28+
* @return {string}
29+
*/
30+
var toGoatLatin = function(S) {
31+
return S.split(' ').map((word, index) => {
32+
return `${word.replace(/^([^aeiou])(.*)/ig, '$2$1')}ma${'a'.repeat(index + 1)}`;
33+
}).join(' ');
34+
};

0 commit comments

Comments
 (0)