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.
1 <= ransomNote.length, magazine.length <= 10^5ransomNote and magazine consist of lowercase English letters.No developer has claimed the Java throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
This question is equivalent to asking whether magazine can contain all the characters in ransomNote.
First count magazine to get the number of words corresponding to each character, and store the result in Map. Each time is an addition one operation.
What to do next?
Traverses Click to view the answer
ransomNote and subtracts one from the number corresponding to the current character (reverse operation). If the number of a character is less than 0, return false.
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
var charToCount = new HashMap<Character, Integer>();
for (var character : magazine.toCharArray()) {
charToCount.put(character, charToCount.getOrDefault(character, 0) + 1);
}
for (var character : ransomNote.toCharArray()) {
charToCount.put(character, charToCount.getOrDefault(character, 0) - 1);
if (charToCount.get(character) < 0) {
return false;
}
}
return true;
}
}
No developer has claimed the Python throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
This question is equivalent to asking whether magazine can contain all the characters in ransomNote.
First count magazine to get the number of words corresponding to each character, and store the result in Map. Each time is an addition one operation.
What to do next?
Traverses Click to view the answer
ransomNote and subtracts one from the number corresponding to the current character (reverse operation). If the number of a character is less than 0, return false.
# from collections import defaultdict
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
char_to_count = defaultdict(int)
for char in magazine:
char_to_count[char] += 1
for char in ransomNote:
char_to_count[char] -= 1
if char_to_count[char] < 0:
return False
return True
No developer has claimed the Javascript throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
This question is equivalent to asking whether magazine can contain all the characters in ransomNote.
First count magazine to get the number of words corresponding to each character, and store the result in Map. Each time is an addition one operation.
What to do next?
Traverses Click to view the answer
ransomNote and subtracts one from the number corresponding to the current character (reverse operation). If the number of a character is less than 0, return false.
var canConstruct = function (ransomNote, magazine) {
const charToCount = new Map()
for (const character of magazine) {
charToCount.set(character, (charToCount.get(character) || 0) + 1)
}
for (const character of ransomNote) {
charToCount.set(character, (charToCount.get(character) || 0) - 1)
if (charToCount.get(character) < 0) {
return false
}
}
return true
};
No developer has claimed the C# throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
This question is equivalent to asking whether magazine can contain all the characters in ransomNote.
First count magazine to get the number of words corresponding to each character, and store the result in Map. Each time is an addition one operation.
What to do next?
Traverses Click to view the answer
ransomNote and subtracts one from the number corresponding to the current character (reverse operation). If the number of a character is less than 0, return false.
public class Solution
{
public bool CanConstruct(string ransomNote, string magazine)
{
var charToCount = new Dictionary<char, int>();
foreach (char character in magazine)
charToCount[character] = charToCount.GetValueOrDefault(character, 0) + 1;
foreach (char character in ransomNote)
{
charToCount[character] = charToCount.GetValueOrDefault(character, 0) - 1;
if (charToCount[character] < 0)
{
return false;
}
}
return true;
}
}
No developer has claimed the Ruby throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
This question is equivalent to asking whether magazine can contain all the characters in ransomNote.
First count magazine to get the number of words corresponding to each character, and store the result in Map. Each time is an addition one operation.
What to do next?
Traverses Click to view the answer
ransomNote and subtracts one from the number corresponding to the current character (reverse operation). If the number of a character is less than 0, return false.
def can_construct(ransom_note, magazine)
char_to_count = Hash.new(0)
magazine.each_char { |c| char_to_count[c] += 1 }
ransom_note.each_char do |c|
char_to_count[c] -= 1
return false if char_to_count[c] < 0
end
true
end
No developer has claimed the Go throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
This question is equivalent to asking whether magazine can contain all the characters in ransomNote.
First count magazine to get the number of words corresponding to each character, and store the result in Map. Each time is an addition one operation.
What to do next?
Traverses Click to view the answer
ransomNote and subtracts one from the number corresponding to the current character (reverse operation). If the number of a character is less than 0, return false.
func canConstruct(ransomNote string, magazine string) bool {
charToCount := make(map[rune]int)
for _, char := range magazine {
charToCount[char]++
}
for _, char := range ransomNote {
charToCount[char]--
if charToCount[char] < 0 {
return false
}
}
return true
}
No developer has claimed the C++ throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
This question is equivalent to asking whether magazine can contain all the characters in ransomNote.
First count magazine to get the number of words corresponding to each character, and store the result in Map. Each time is an addition one operation.
What to do next?
Traverses Click to view the answer
ransomNote and subtracts one from the number corresponding to the current character (reverse operation). If the number of a character is less than 0, return false.
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
unordered_map<char, int> char_to_count;
for (char character : magazine) {
char_to_count[character]++;
}
for (char character : ransomNote) {
char_to_count[character]--;
if (char_to_count[character] < 0) {
return false;
}
}
return true;
}
};
No developer has claimed the Rust throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.