[LeetCode]383. Ransom Note
题目
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note:
You may assume that both strings contain only lowercase letters.
1 | canConstruct("a", "b") -> false |
难度
Easy
方法
遍历2个字符串,索引分别为i
, j
,如果ransomNote[i] == magazine[j]
,则i++
, j++
,否则j++
。如果i == len(ransomNote)
,则表示能够用magazine
的字符组成ransomNote
,否则则不能
python代码
1 | class Solution(object): |