1270. Letter of blackmail

1270. Letter of blackmail

Chinese English

Given an arbitrary string representing the content of the extortion letter, and another string representing the contents of the magazine, write a method to determine whether the extortion letter can be constructed by cutting out the contents of the magazine, and if so, return true; Otherwise it returns false.

Each character in the magazine string can only be used once in the extortion letter.

Sample

Sample 1

输入 : ransomNote = "aa", magazine = "aab"
输出 : true
解析 : 勒索信的内容可以有杂志内容剪辑而来

Sample 2

输入 : ransomNote = "aaa", magazine = "aab"
输出 : false
解析 : 勒索信的内容无法从杂志内容中剪辑而来

Precautions

You can think of both strings as containing only lowercase letters.

 
 
Enter test data (one parameter per line) How to understand test data?
class Solution:
     "" "
     @param ransomNote: a string 
    @param magazine: a string 
    @return: whether the ransom note can be constructed from the magazines
     " ""
     '' '
     General idea:
     1. Loop ransomNote to determine whether the current character is magazine, if there is one, remove one, and all will eventually return True, otherwise Flaase
     '' '
     def canConstruct (self, ransomNote, magazine):
         for i in ransomNote:
             if i in magazine:
                magazine = magazine.replace(i,'',1)
            else:
                return False
        return True

 

Guess you like

Origin www.cnblogs.com/yunxintryyoubest/p/12729666.html