Content-Length: 7748 | pFad | https://leetcode-in-net.github.io/LeetCodeNet/G0301_0400/S0383_ransom_note
Easy
Given two stings ransomNote
and magazine
, return true
if ransomNote
can be constructed 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
and magazine
consist of lowercase English letters.public class Solution {
public bool CanConstruct(string ransomNote, string magazine) {
int[] count = new int[26];
foreach (char c in magazine) count[c - 'a']++;
foreach (char c in ransomNote) {
if (--count[c - 'a'] < 0) return false;
}
return true;
}
}
Fetched URL: https://leetcode-in-net.github.io/LeetCodeNet/G0301_0400/S0383_ransom_note
Alternative Proxies: