Created
May 30, 2017 09:01
-
-
Save iulianu/a57ea855e6b4e288bba18dc738636c76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.annotation.tailrec | |
import scala.collection.JavaConverters._ | |
// you can write to stdout for debugging purposes, e.g. | |
// println("this is a debug message") | |
object Task2Solution { | |
class TransformationIterator(s: String, removable: String) extends Iterator[String] { | |
var i = 0 | |
val remLen = removable.length | |
val sLen = s.length | |
shift() | |
override def hasNext = i < sLen && i != -1 | |
override def next = { | |
val transformed = s.substring(0, i) + s.substring(i + remLen, sLen) | |
shift() | |
transformed | |
} | |
private def shift(): Unit = { | |
i = s.indexOfSlice(removable, i) | |
} | |
} | |
def possibleTransformations(s: String): Iterator[String] = { | |
new TransformationIterator(s, "AA") ++ | |
new TransformationIterator(s, "BB") ++ | |
new TransformationIterator(s, "CC") | |
} | |
@tailrec | |
def transform(s: String): String = { | |
val transformations = possibleTransformations(s) | |
if(!transformations.hasNext) | |
s | |
else | |
transform(transformations.next()) | |
} | |
def solution(s: String): String = { | |
transform(s) | |
} | |
} | |
object Main3 extends App { | |
// println(Solution.solution("ACCAABBC")) | |
// println(Solution.solution("ABCBBCBA")) | |
println(Task2Solution.solution("A")) | |
// val longStr = "BBAACC" * 1000 | |
// println(Task2Solution.solution(longStr)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment