Easy to understand Python solution

  • 0

    Build the hash table first for two strings and find whether counts of ransomNote are right below those of magazine.

    class Solution(object):
        def canConstruct(self, ransomNote, magazine):
            return False not in [i <= j for (i,j) in zip(self.hashTable(ransomNote),self.hashTable(magazine))]
        def hashTable(self,str):
            lst = [0] * 26
            for i in xrange(len(str)):
                lst[ord(str[i])-ord('a')] += 1
            return lst

Log in to reply

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