Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun stringChallenge(str: String): String {
- val result = recurseChallenge(str)
- return result.length.toString()
- }
- fun recurseChallenge(string: String): String {
- val firstChar = string.first()
- val isAllTheSame = string.all { it == firstChar }
- if (isAllTheSame) {
- return string
- } else {
- var position = 0
- for (index in 0 until string.length - 1) {
- val curValue = string[index]
- val nextValue = string[index + 1]
- if (curValue != nextValue) {
- position = index
- break
- }
- }
- val oldSequence = string[position] + string[position + 1].toString()
- val newString =
- string.replaceFirst(
- oldSequence,
- checkNeedReplaceOrNot(string[position], string[position + 1]),
- true
- )
- println(newString)
- return recurseChallenge(newString)
- }
- }
- fun checkNeedReplaceOrNot(firstChar: Char, secondChar: Char): String = if (firstChar == secondChar) {
- firstChar.toString() + secondChar.toString()
- } else {
- thirdChar(firstChar, secondChar)
- }
- fun thirdChar(firstChar: Char, secondChar: Char): String {
- val twoChar: String = firstChar.toString() + secondChar.toString()
- if (!twoChar.contains("a")) {
- return "a"
- }
- if (!twoChar.contains("b")) {
- return "b"
- }
- if (!twoChar.contains("c")) {
- return "c"
- }
- return twoChar
- }
- fun main(args: Array<String>) {
- println(stringChallenge(readLine()!!))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement