Skip to content

Commit 56dbdf5

Browse files
committed
[LeetCode Sync] Runtime - 16 ms (61.68%), Memory - 17.9 MB (54.26%)
1 parent 0b5b59f commit 56dbdf5

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p>
2+
3+
<p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
<pre><strong>Input:</strong> ransomNote = "a", magazine = "b"
8+
<strong>Output:</strong> false
9+
</pre><p><strong class="example">Example 2:</strong></p>
10+
<pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab"
11+
<strong>Output:</strong> false
12+
</pre><p><strong class="example">Example 3:</strong></p>
13+
<pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab"
14+
<strong>Output:</strong> true
15+
</pre>
16+
<p>&nbsp;</p>
17+
<p><strong>Constraints:</strong></p>
18+
19+
<ul>
20+
<li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li>
21+
<li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li>
22+
</ul>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
3+
count = Counter(magazine)
4+
for letter in ransomNote:
5+
if count[letter] <= 0:
6+
return False
7+
count[letter] -= 1
8+
9+
return True

0 commit comments

Comments
 (0)