class PigLatin {
    companion object {
        fun translate(inpString: String): String {
            val stringList: List<String> = inpString.split(' ')
            val returnStringBuilder = StringBuilder()
            for (i in stringList){
                returnStringBuilder.append(i.convertToPigLatin()).append(" ")
            }
            return String(returnStringBuilder).trim()
        }

    }
}

fun Char.isVowel(): Boolean{
    val lowercase = this.toLowerCase()
    return ((lowercase == 'a') or (lowercase == 'e') or (lowercase == 'i') or (lowercase == 'o') or (lowercase == 'u'))
}

fun String.convertToPigLatin(): String{
    val stringBuilder = StringBuilder()
    val firstchar = this.first()
    if (firstchar.isVowel()){
        stringBuilder.append(this + "ay")
    }
    else if ((!firstchar.isVowel()) and (this.substring(1..2) == "qu")){
        stringBuilder.append(this.substring(3) + this.substring(0..2) + "ay")
    }
    else if (this.substring(0..2) == "sch"){
        stringBuilder.append(this.substring(3) + "sch" + "ay")
    }
    else if (this.substring(0..2) == "thr"){
        stringBuilder.append(this.substring(3) + "thr" + "ay")
    }
    else {
        when (this.substring(0..1)){
            "qu" -> stringBuilder.append(this.substringAfter("qu") + "qu" + "ay")
            "ch" -> stringBuilder.append(this.substringAfter("ch") + "ch" + "ay")
            "th" -> stringBuilder.append(this.substringAfter("th") + "th" + "ay")
            "yt" -> stringBuilder.append(this + "ay")
            "xr" -> stringBuilder.append(this + "ay")
            else -> {val (first, temp) = this.partition { it === this.first() }
                stringBuilder.append(temp + first + "ay")}
        }
    }
    return String(stringBuilder)
}