arcbjorn

thoughtbook

Thursday, January 5, 2023

Goat latin

Leetcode 824 problem.

Go

  • Time complexity: O(n2)O(n^{2}) - n a the length of a string
  • Auxiliary space: O(n)O(n) - n is a length of a string
import "fmt"

func containsChar(arr []string, target string) bool {
    for _, item := range arr {
        if item == target {
            return true
        }
    }

    return false
}

func toGoatLatin(sentence string) string {
    words := strings.Split(sentence, " ")
    vowels := []string{"a", "e", "i", "o", "u", "A", "E", "I", "O", "U"}

    var transformedSentence string

    for index, word := range words {
        newWord := word
        firstLetter := string(word[0])

        if (containsChar(vowels, firstLetter)) {
            newWord += "ma"
        } else {
            newWord = newWord[1:] + firstLetter + "ma"
        }

        for i := 0; i <= index; i++ {
            newWord += "a"
        }

        transformedSentence += newWord

        if (index != len(words)-1) {
            transformedSentence += " "
        }
    }

    return transformedSentence
}
Creative Commons Licence