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.
Example 1:
1 | Input: ransomNote = "a", magazine = "b" |
Example 2:
1 | Input: ransomNote = "aa", magazine = "ab" |
Example 3:
1 | Input: ransomNote = "aa", magazine = "aab" |
Constraints:
- You may assume that both strings contain only lowercase letters.
hashmap
首先要理解题意, 这题意我理解了半天. 被ransom note, magazine这些名词弄蒙了.
这题的意思就是能不能从magazine的所有字符中挑选出一些字符来组成ransom note!
先把magazine所有字符放一个(字符 -> 字符数量)map里面, 然后判断map里面的能不能组成ransom note即可.
但是使用哈希表只超过了5%的空间占用. 因为只有英文小写字母, 可以考虑用一个int[26]的数组来代替map, 空间占用应该会低一些
1 | class Solution { |