python solution using ord()

  • 0
    def canConstruct(self, ransomNote, magazine):
            s= ord('A')
            ct = [0]*64
            for l in magazine:
                ct[ord(l)-s] += 1
            for l in ransomNote:
                ct[ord(l)-s] -= 1
                if ct[ord(l)-s]<0: return False
            return True

    where ord(l) gives the corresponding index of l in ASCII code

Log in to reply

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