Skip to content

Commit b4cbb30

Browse files
committed
Runtime 59 ms (Top 81.28%) | Memory 42.0 MB (Top 13.37%)
1 parent 858197e commit b4cbb30

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed
+29-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
1-
// Runtime: 94 ms (Top 44.83%) | Memory: 42.2 MB (Top 73.56%)
2-
ar toGoatLatin = function(sentence) {
3-
let arr = sentence.split(' ')
4-
let vowels = ['a', 'e', 'i', 'o', 'u']
5-
for (let i=0; i< arr.length; i++) {
6-
if (vowels.includes(arr[i][0].toLowerCase())) {
7-
arr[i] = arr[i] + 'ma'
8-
} else {
9-
arr[i] = arr[i].substring(1) + arr[i].substring(0,1) + 'ma'
1+
/**
2+
* @param {string} S
3+
* @return {string}
4+
*/
5+
var toGoatLatin = function(S) {
6+
let re = /[aeiou]/gi
7+
let word = S.split(' ')
8+
let add = 0
9+
let arr = []
10+
11+
for(let i= 0; i < word.length; i++) {
12+
if(!word[i].substring(0,1).match(re)) {
13+
arr = word[i].split('')
14+
let letter = arr.shift()
15+
arr.push(letter)
16+
word[i] = arr.join('')
1017
}
11-
12-
arr[i] = arr[i] + 'a'.repeat(i+1)
13-
}
14-
15-
return arr.join(' ')
18+
19+
// have to append 'ma' regardless if it starts with a vowel
20+
word[i] += 'ma'
21+
22+
// append 'a' number of times as the index + 1
23+
add = i+1
24+
while(add >0) {
25+
word[i] += 'a'
26+
add--
27+
}
28+
}
29+
30+
return word.join(' ')
1631
};

0 commit comments

Comments
 (0)