Goat Latin

Easy problem with a short solution.

class Solution {
    
    private val vowels = listOf('a', 'e', 'i', 'o', 'u')
    
    fun toGoatLatin(S: String): String {
        val words = S.split(" ")
        var builder = StringBuilder()
        
        words.forEachIndexed { index, word ->
            var localBuilder = StringBuilder()
            
            when {
                word[0].toLowerCase() in vowels -> localBuilder.append(word + "ma")
                !(word[0].toLowerCase() in vowels) -> localBuilder.append(word.substring(1) + word[0] + "ma")                
            }    
            
            repeat(index + 1){
                localBuilder.append("a")
            }
            
            builder.append(localBuilder.toString() + " ")
        }
        
        return builder.toString().trim()
        
    }
}
Runtime: 0 ms, faster than 100.00% of Kotlin online submissions for Goat Latin.
Memory Usage: 36.5 MB, less than 100.00% of Kotlin online submissions for Goat Latin.

This is straightforward coding. Very easy.

comments powered by Disqus