am facing dramatic performances issues for a kind of classic problem to solve.
i have as inputs a string like :
input :
val test: String = "1,1,12,1,1,12,13"
val test2: String = "1,1,12,1,1,12,13,1,1,12"
Desired output:
test --> 2{1,1,12},13
test2 --> 2{1,1,12},13,2{1},12
I have took the following approach, by comparing suffix, and prefix of that given string in a recursive fashion
def shorten_prefix(pfx:String, sfx: String): (Integer, String, String) = {
var count = 1
var _sfx = sfx
while (_sfx.startsWith(pfx)) {
count = count + 1
_sfx = _sfx.splitAt(pfx.length)._2
}
val _tmp = s"$count{${pfx}}"
(count, _tmp, _sfx)
}
def find_shortest_repr(input: String): String= {
var possible_variants: mutable.ListBuffer[String] = new mutable.ListBuffer[String]()
if (input.isEmpty) {
return ""
}
val size = input.length
for (i <- 1 until size + 1){
val (prefixes, suffixes) = input.splitAt(i)
val (counter, shortened, new_ending) = shorten_prefix(prefixes, suffixes)
val shortest_ending: String = find_shortest_repr(new_ending)
val tmp = shortened ++ shortest_ending
possible_variants += (tmp)
}
possible_variants.minBy(f => f.length)
}
def compress(x: String)= {
val _tmp = find_shortest_repr(x)
val regex = "(,\\})"
val subst = "},"
_tmp.replaceAll(regex, subst).dropRight(1)
}
println(compress(test))
println(compress(test2))
}
It sounds that the longer is the string, longer is the calculation of all possible permutations, to find the best match.
Is there any tricks or advice, am missing ?
Thanks you