Scala solution


  • 0
    M

    Learning Scala. Suggestions are more than welcome.

    object Solution {
        def canConstruct(ransomNote: String, magazine: String): Boolean = {
          if (magazine.size < ransomNote.size) return false
          val Array(map1, map2) = Array(ransomNote, magazine).map(_.toCharArray.groupBy(identity).mapValues(_.size))
          if (!(map1.keySet subsetOf map2.keySet)) return false
          map2.transform((k, v) => v - map1.getOrElse(k, 0)).filter(_._2 < 0).size <= 0
        }
    }
    

Log in to reply
 

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