arunavasaha90
BAN USERHow about the below, written in scala.
def main(args: Array[String]): Unit = {
val words = Array("abc", "baa", "caan", "an", "banc")
val chars = Array("a", "a", "n", "c", "b")
//breaking each word in words into a pair of it's chars and their existence (initially zero).
val wordMapList = words.sortWith(_ > _).map(_.map(_.toString -> 0))
//iterating on chars array and marking current chars existence in the previous list if string is present and the char is not already used.
val r = chars.foldLeft(wordMapList)((b, a) => b.map(w => if (w.exists(e => e._1 == a && e._2 == 0)) w.map(x => if (x._1 == a) (x._1, x._2 + 1) else x) else w))
println(r.find(_.forall(_._2 == 1)).map(_.foldLeft("")((b, a) => s"$b${a._1}")).getOrElse("NotFound"))
}
Hi @careercup, could you please format my code? I followed your suggestion and wrapped the code part with
but that didn't help. Please help as am new here :)
- arunavasaha90 November 09, 2017