A python solution with description


  • 0
    J

    Here is my approach to solve this problem.
    Remove letters in ransom from magazine one by one.
    During this process, If there is any letter that does not exists in magazine return False.
    If all matches, then return True

    class Solution(object):
        def canConstruct(self, ransomNote, magazine):
            """
            :type ransomNote: str
            :type magazine: str
            :rtype: bool
            """
            magazine = map(lambda x:x, magazine)
    
            
            for i in range(len(ransomNote)):
                ch = ransomNote[i]
                if not magazine:
                    return False
                
                if ch in magazine:
                    magazine.remove(ch)
                else:
                    return False
                
            return True
    

Log in to reply
 

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