Skip to content

Commit 7b3b774

Browse files
solves goat latin
1 parent 64bc96b commit 7b3b774

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-191/2081-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-191/2081-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-192/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-192/2081-1abc9c.svg)
55
![problems-solved-python](https://img.shields.io/badge/Python-186/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
@@ -222,7 +222,7 @@
222222
| 812 | [Largest Triangle Area](https://leetcode.com/problems/largest-triangle-area) | [![Java](assets/java.png)](src/LargestTriangleArea.java) |
223223
| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word) | [![Java](assets/java.png)](src/MostCommonWord.java) |
224224
| 821 | [Shortest Distance to Character](https://leetcode.com/problems/shortest-distance-to-a-character) | [![Java](assets/java.png)](src/ShortestDistanceToACharacter.java) |
225-
| 824 | [Goat Latin](https://leetcode.com/problems/goat-latin) | |
225+
| 824 | [Goat Latin](https://leetcode.com/problems/goat-latin) | [![Java](assets/java.png)](src/GoatLatin.java) |
226226
| 830 | [Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups) | |
227227
| 832 | [Flipping an Image](https://leetcode.com/problems/flipping-an-image) | |
228228
| 836 | [Rectangle Overlap](https://leetcode.com/problems/rectangle-overlap) | |

src/GoatLatin.java

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.util.Set;
2+
3+
public class GoatLatin {
4+
private static final Set<Character> VOWELS = Set.of('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');
5+
6+
public String toGoatLatin(String sentence) {
7+
StringBuilder result = new StringBuilder(), word = new StringBuilder();
8+
char firstLetter = ' ';
9+
int wordCount = 1;
10+
boolean vowelWord = false, newWord = true;
11+
for (int index = 0 ; index < sentence.length() ; index++) {
12+
if (newWord) {
13+
if (index == sentence.length() - 1) {
14+
result.append(toGoatLatin(wordCount, new StringBuilder(),
15+
false, sentence.charAt(index)));
16+
break;
17+
}
18+
if (isVowel(sentence.charAt(index))) {
19+
vowelWord = true;
20+
word.append(sentence.charAt(index));
21+
} else {
22+
vowelWord = false;
23+
firstLetter = sentence.charAt(index);
24+
}
25+
26+
newWord = false;
27+
} else if (sentence.charAt(index) == ' ' || index == sentence.length() - 1) {
28+
newWord = true;
29+
if (index == sentence.length() - 1) word.append(sentence.charAt(index));
30+
result.append(toGoatLatin(wordCount, word, vowelWord, firstLetter));
31+
word = new StringBuilder();
32+
wordCount++;
33+
} else {
34+
word.append(sentence.charAt(index));
35+
}
36+
}
37+
return result.toString();
38+
}
39+
40+
private StringBuilder toGoatLatin(int wordCount, StringBuilder word, boolean vowelWord, char firstLetter) {
41+
return new StringBuilder()
42+
.append(wordCount == 1 ? "" : ' ')
43+
.append(word)
44+
.append(vowelWord ? "" : firstLetter)
45+
.append("ma")
46+
.append("a".repeat(wordCount));
47+
}
48+
49+
private boolean isVowel(char character) {
50+
return VOWELS.contains(character);
51+
}
52+
}

0 commit comments

Comments
 (0)