Brute force that somehow is faster


  • 0
    Y

    Somehow my brute force method is faster than the suggested python solution. It does cost memory if the string is large, but it didn't say that's a restraint.

        def canConstruct(self, ransomNote, magazine):
            """
            :type ransomNote: str
            :type magazine: str
            :rtype: bool
            """
            for i in ransomNote:
                if i not in magazine:
                    return False
                else:
                    magazine = magazine.replace(i, '', 1)
            return True

Log in to reply
 

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