import scala.collection.convert.ImplicitConversions.`collection asJava`
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
object Solution {
def to_string(arr: Array[String]): String = {
var str = "["
for (i <- 0 until arr.length) {
str += String.valueOf(arr(i))
if (i + 1 < arr.length) str += ", "
}
str += "]"
str
}
def smallestSequence(listA: Array[String], listB: Array[String]): Array[String] = { // Returns an empty list if one or more list is empty.
if (listA.length == 0 || listB.length == 0) return new Array[String](0)
// We will use this dictionary to keep a count of all unique topics in list b.
val dictListB = new mutable.HashMap[String, Int]()
{
override def default(key: String): Int = 0
}
for (i <- 0 until listB.length) {
val count = dictListB(listB(i))
dictListB.put(listB(i), count + 1)
}
// This will hold the count of number of unique topics in list b.
val countUniqueB = dictListB.size
// uniqueCharacters keeps a count of the number of unique characters of list_b which are present in the current with its required frequency.
var uniqueCharacters = 0
// This dictionary holds the count of all the unique topics in the current window.
val uniqueTopics = new mutable.HashMap[String, Int]() {
override def default(k: String): Int = 0
}
// Checks and appends a topic along with its index from list A in sifted list, if the topic is present in list B.
val siftedListA = new ListBuffer[mutable.HashMap[Int, String]]();
for (i <- 0 until listA.length) {
val str = listA(i)
if (dictListB.contains(str)) {
val tempMap = new mutable.HashMap[Int, String]()
tempMap.put(i, str)
siftedListA.addOne(tempMap)
}
}