• + 0 comments

    this was easy, scala:

    import scala.io.StdIn
    
    object Solution {
      private val wrongPassword = "WRONG PASSWORD"
    
      private def validateLogin(loginAttempt: String, passwords: Seq[String]): String = {
        def parse(remaining: String): Option[Seq[String]] = {
          if (remaining.isEmpty)
            return Some(Nil)
          passwords
            .find(remaining.startsWith)
            .flatMap { token => parse(remaining.substring(token.length)).map(token +: _) }
        }
        parse(loginAttempt)
          .map(_.mkString(" "))
          .getOrElse(wrongPassword)
      }
    
      def main(args: Array[String]): Unit = {
        val in = Iterator
          .continually(StdIn.readLine())
          .takeWhile(null != _)
          .map(_.trim)
        val t = in.next().toInt
        (1 to t)
          .map { _ =>
            val n = in.next().toInt
            val passwords = in.next().split(' ').toSeq
            val loginAttempt = in.next()
            validateLogin(loginAttempt, passwords)
          }
          .foreach(println)
      }
    }