Skip to content

Commit 8fd26b9

Browse files
committed
add: Goat Latin
1 parent 483fcdc commit 8fd26b9

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

β€Ž0824-goat-latin.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
struct Solution;
2+
3+
const VOWELS: [u8; 10] = [b'a', b'e', b'i', b'o', b'u', b'A', b'E', b'I', b'O', b'U'];
4+
impl Solution {
5+
pub fn to_goat_latin(s: String) -> String {
6+
let mut result = Vec::new();
7+
let mut repeat = 1_usize;
8+
for word in s.split_ascii_whitespace() {
9+
if VOWELS.iter().any(|&v| v == word.as_bytes()[0]) {
10+
result.push(word.to_string());
11+
} else {
12+
let (left, right) = word.split_at(1);
13+
result.push(format!("{}{}", right, left));
14+
}
15+
if let Some(last) = result.last_mut() {
16+
*last = format!("{}ma{}", last, "a".repeat(repeat));
17+
}
18+
repeat += 1;
19+
}
20+
result.join(" ")
21+
}
22+
}
23+
24+
fn main() {
25+
assert_eq!(
26+
"Imaa peaksmaaa oatGmaaaa atinLmaaaaa".to_string(),
27+
Solution::to_goat_latin("I speak Goat Latin".to_string())
28+
);
29+
assert_eq!(
30+
"heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa".to_string(),
31+
Solution::to_goat_latin("The quick brown fox jumped over the lazy dog".to_string())
32+
);
33+
}

0 commit comments

Comments
 (0)