comments | difficulty | edit_url | tags | |||
---|---|---|---|---|---|---|
true |
Easy |
|
Given two strings ransomNote
and magazine
, return true
if ransomNote
can be constructed by using the letters from magazine
and false
otherwise.
Each letter in magazine
can only be used once in ransomNote
.
Example 1:
Input: ransomNote = "a", magazine = "b" Output: false
Example 2:
Input: ransomNote = "aa", magazine = "ab" Output: false
Example 3:
Input: ransomNote = "aa", magazine = "aab" Output: true
Constraints:
1 <= ransomNote.length, magazine.length <= 105
ransomNote
andmagazine
consist of lowercase English letters.
We can use a hash table or an array magazine
. Then traverse the string ransomNote
, for each character magazine
is not enough, so it cannot be composed of ransomNote
, just return
Otherwise, after the traversal, it means that each character in ransomNote
can be found in magazine
. Therefore, return
The time complexity is ransomNote
and magazine
respectively; and
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
cnt = Counter(magazine)
for c in ransomNote:
cnt[c] -= 1
if cnt[c] < 0:
return False
return True
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] cnt = new int[26];
for (int i = 0; i < magazine.length(); ++i) {
++cnt[magazine.charAt(i) - 'a'];
}
for (int i = 0; i < ransomNote.length(); ++i) {
if (--cnt[ransomNote.charAt(i) - 'a'] < 0) {
return false;
}
}
return true;
}
}
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int cnt[26]{};
for (char& c : magazine) {
++cnt[c - 'a'];
}
for (char& c : ransomNote) {
if (--cnt[c - 'a'] < 0) {
return false;
}
}
return true;
}
};
func canConstruct(ransomNote string, magazine string) bool {
cnt := [26]int{}
for _, c := range magazine {
cnt[c-'a']++
}
for _, c := range ransomNote {
cnt[c-'a']--
if cnt[c-'a'] < 0 {
return false
}
}
return true
}
function canConstruct(ransomNote: string, magazine: string): boolean {
const cnt: number[] = Array(26).fill(0);
for (const c of magazine) {
++cnt[c.charCodeAt(0) - 97];
}
for (const c of ransomNote) {
if (--cnt[c.charCodeAt(0) - 97] < 0) {
return false;
}
}
return true;
}
public class Solution {
public bool CanConstruct(string ransomNote, string magazine) {
int[] cnt = new int[26];
foreach (var c in magazine) {
++cnt[c - 'a'];
}
foreach (var c in ransomNote) {
if (--cnt[c - 'a'] < 0) {
return false;
}
}
return true;
}
}
class Solution {
/**
* @param String $ransomNote
* @param String $magazine
* @return Boolean
*/
function canConstruct($ransomNote, $magazine) {
$arrM = str_split($magazine);
for ($i = 0; $i < strlen($magazine); $i++) {
$hashtable[$arrM[$i]] += 1;
}
for ($j = 0; $j < strlen($ransomNote); $j++) {
if (!isset($hashtable[$ransomNote[$j]]) || $hashtable[$ransomNote[$j]] == 0) {
return false;
} else {
$hashtable[$ransomNote[$j]] -= 1;
}
}
return true;
}
}