Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom 
note can be constructed from the magazines ; otherwise, it will return false. 


Each letter in the magazine string can only be used once in your ransom note.


Note:
You may assume that both strings contain only lowercase letters.



canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true

Source: leetcode | Difficulty: EASY
Tags: String algorithms

Data structures

Mathematics

Starter code: class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        

Solutions:
class Solution:

	def canConstruct(self, ransomNote, magazine):
		ransome = set(ransomNote)
		for i in ransome:
			if ransomNote.count(i) > magazine.count(i):
				return False
		return True


class Solution:

	def canConstruct(self, ransomNote, magazine):
		n = [0] * 128
		for c in magazine:
			n[ord(c)] += 1
		for c in ransomNote:
			v = n[ord(c)]
			if v == 0:
				return False
			n[ord(c)] = v - 1
		return True


class Solution:

	def canConstruct(self, ransomNote, magazine):
		return all((ransomNote.count(i) <= magazine.count(i) for i in set(ransomNote)))


class Solution:

	def canConstruct(self, ransomNote, magazine):
		for i in set(ransomNote):
			if i not in set(magazine) or ransomNote.count(i) > magazine.count(i):
				return False
		return True


class Solution:

	def canConstruct(self, ransomNote, magazine):
		available_letters = dict()
		for c in magazine:
			if c in list(available_letters.keys()):
				available_letters[c] += 1
			else:
				available_letters[c] = 1
		for c in ransomNote:
			if c not in list(available_letters.keys()):
				return False
			else:
				available_letters[c] -= 1
				if available_letters[c] == 0:
					del available_letters[c]
		return True


class Solution:

	def canConstruct(self, ransomNote, magazine):
		for note in ransomNote:
			if note not in magazine:
				return False
			else:
				INDEX = magazine.index(note)
				magazine = magazine[:INDEX] + magazine[INDEX + 1:]
		return True


class Solution:

	def canConstruct(self, ransomNote, magazine):
		a = set(ransomNote)
		b = set(magazine)
		for element in a:
			if ransomNote.count(element) > magazine.count(element):
				return False
		return True


class Solution:

	def canConstruct(self, ransomNote, magazine):
		from collections import Counter
		ransom = Counter(ransomNote)
		mag = Counter(magazine)
		return all((mag[letter] >= ransom[letter] for letter in ransom))


class Solution:

	def canConstruct(self, ransomNote, magazine):
		dic1 = collections.Counter(ransomNote)
		dic2 = collections.Counter(magazine)
		for key in dic1:
			if key not in dic2 or dic2[key] < dic1[key]:
				return False
		return True


class Solution:

	def canConstruct(self, ransomNote, magazine):
		a = collections.Counter(ransomNote)
		b = collections.Counter(magazine)
		for let in ransomNote:
			if a[let] > b[let]:
				return False
		return True


class Solution:

	def canConstruct(self, ransomNote, magazine):
		magazine = list(magazine)
		for letter in ransomNote:
			try:
				magazine.remove(letter)
			except ValueError:
				return False
		return True


class Solution:

	def canConstruct(self, ransomNote, magazine):
		rs = list(ransomNote)
		ms = list(magazine)
		for r in rs:
			if r not in ms:
				return False
			else:
				ms.remove(r)
		return True


class Solution:

	def canConstruct(self, ransomNote, magazine):
		r_map = {}
		for char in ransomNote:
			if char in r_map:
				r_map[char] += 1
			else:
				r_map[char] = 1
		m_map = {}
		for char in magazine:
			if char in m_map:
				m_map[char] += 1
			else:
				m_map[char] = 1
		for (char, count) in r_map.items():
			if char not in m_map:
				return False
			if count > m_map[char]:
				return False
		return True