Skip to content

Commit 8eca353

Browse files
committed
feat: add solutions to lc problem: No.0824
No.0824.Goat Latin
1 parent 7dbfd9e commit 8eca353

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

solution/0800-0899/0824.Goat Latin/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,53 @@ class Solution {
9999
}
100100
```
101101

102+
### **TypeScript**
103+
104+
```ts
105+
function toGoatLatin(sentence: string): string {
106+
return sentence
107+
.split(' ')
108+
.map((s, i) => {
109+
let startStr: string;
110+
if (/[aeiou]/i.test(s[0])) {
111+
startStr = s;
112+
} else {
113+
startStr = s.slice(1) + s[0];
114+
}
115+
return `${startStr}ma${'a'.repeat(i + 1)}`;
116+
})
117+
.join(' ');
118+
}
119+
```
120+
121+
### **Rust**
122+
123+
```rust
124+
use std::collections::HashSet;
125+
impl Solution {
126+
pub fn to_goat_latin(sentence: String) -> String {
127+
let set: HashSet<&char> = ['a', 'e', 'i', 'o', 'u'].into_iter().collect();
128+
sentence
129+
.split_whitespace()
130+
.enumerate()
131+
.map(|(i, s)| {
132+
let first = char::from(s.as_bytes()[0]);
133+
let mut res = if set.contains(&first.to_ascii_lowercase()) {
134+
s.to_string()
135+
} else {
136+
s[1..].to_string() + &first.to_string()
137+
};
138+
res.push_str("ma");
139+
res.push_str(&"a".repeat(i + 1));
140+
res
141+
})
142+
.into_iter()
143+
.collect::<Vec<String>>()
144+
.join(" ")
145+
}
146+
}
147+
```
148+
102149
### **...**
103150

104151
```

solution/0800-0899/0824.Goat Latin/README_EN.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,53 @@ class Solution {
9494
}
9595
```
9696

97+
### **TypeScript**
98+
99+
```ts
100+
function toGoatLatin(sentence: string): string {
101+
return sentence
102+
.split(' ')
103+
.map((s, i) => {
104+
let startStr: string;
105+
if (/[aeiou]/i.test(s[0])) {
106+
startStr = s;
107+
} else {
108+
startStr = s.slice(1) + s[0];
109+
}
110+
return `${startStr}ma${'a'.repeat(i + 1)}`;
111+
})
112+
.join(' ');
113+
}
114+
```
115+
116+
### **Rust**
117+
118+
```rust
119+
use std::collections::HashSet;
120+
impl Solution {
121+
pub fn to_goat_latin(sentence: String) -> String {
122+
let set: HashSet<&char> = ['a', 'e', 'i', 'o', 'u'].into_iter().collect();
123+
sentence
124+
.split_whitespace()
125+
.enumerate()
126+
.map(|(i, s)| {
127+
let first = char::from(s.as_bytes()[0]);
128+
let mut res = if set.contains(&first.to_ascii_lowercase()) {
129+
s.to_string()
130+
} else {
131+
s[1..].to_string() + &first.to_string()
132+
};
133+
res.push_str("ma");
134+
res.push_str(&"a".repeat(i + 1));
135+
res
136+
})
137+
.into_iter()
138+
.collect::<Vec<String>>()
139+
.join(" ")
140+
}
141+
}
142+
```
143+
97144
### **...**
98145

99146
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::collections::HashSet;
2+
impl Solution {
3+
pub fn to_goat_latin(sentence: String) -> String {
4+
let set: HashSet<&char> = ['a', 'e', 'i', 'o', 'u'].into_iter().collect();
5+
sentence
6+
.split_whitespace()
7+
.enumerate()
8+
.map(|(i, s)| {
9+
let first = char::from(s.as_bytes()[0]);
10+
let mut res = if set.contains(&first.to_ascii_lowercase()) {
11+
s.to_string()
12+
} else {
13+
s[1..].to_string() + &first.to_string()
14+
};
15+
res.push_str("ma");
16+
res.push_str(&"a".repeat(i + 1));
17+
res
18+
})
19+
.into_iter()
20+
.collect::<Vec<String>>()
21+
.join(" ")
22+
}
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function toGoatLatin(sentence: string): string {
2+
return sentence
3+
.split(' ')
4+
.map((s, i) => {
5+
let startStr: string;
6+
if (/[aeiou]/i.test(s[0])) {
7+
startStr = s;
8+
} else {
9+
startStr = s.slice(1) + s[0];
10+
}
11+
return `${startStr}ma${'a'.repeat(i + 1)}`;
12+
})
13+
.join(' ');
14+
}

0 commit comments

Comments
 (0)