Scala solution


  • 0
    L
    import scala.collection.mutable.ArrayBuffer
    def getHint(secret: String, guess: String): String = {
          var secretStr = new Array[ArrayBuffer[Int]](10)
          var guessStr = new Array[ArrayBuffer[Int]](10)
        for(i <- 0 to secretStr.length-1 ){
          secretStr(i) = new ArrayBuffer[Int]()
          guessStr(i)= new ArrayBuffer[Int]()
        }
          var bull:Int = 0
          var cows:Int = 0
        for(i<- 0 to secret.length-1){
          val s:Int=secret(i)-'0'
          secretStr(s)+=i
          val g:Int=guess(i)-'0'
          guessStr(g)+=i
        }
        for(i<- 0 to 9){
          if(secretStr(i).length!=0&&guessStr(i).length!=0){
            val sStrL = secretStr(i).length
            val gStrL = guessStr(i).length
            var temp=0
            for(m <- 0 to sStrL-1){
              for(n<- 0 to gStrL -1)
                if(secretStr(i)(m)==guessStr(i)(n))
                  temp=temp+1
            }
                 bull = bull+temp
            if(sStrL<gStrL)
              cows=cows+sStrL-temp
            else
              cows=cows+gStrL-temp
    
          }
        }
        val result:String=bull+"A"+cows+"B"
        result
      }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.