Straight Scala Solution


  • 0
    L
      def canConstruct(ransomNote: String, magazine: String): Boolean = {
          val tempArr = Array.fill(26)(0)
          val result:Boolean=true
        for(i <- 0 to ransomNote.length-1)
          tempArr(ransomNote(i)-'a')-=1
        for(i<- 0 to magazine.length-1)
          tempArr(magazine(i)-'a')+=1
    
        for(i<- 0 to tempArr.length-1)
          if(tempArr(i)<0)
            return false
        result
      }
    

Log in to reply
 

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