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.
Note:
You may assume that both strings contain only lowercase letters.
You may assume that both strings contain only lowercase letters.
canConstruct("a", "b") -> false canConstruct("aa", "ab") -> false canConstruct("aa", "aab") -> true<Solution>
想法如下
- 使用一個 hash map 來記錄 magazine 出現的字元,以及次數
- 檢查在 ransom note 裡面的字元,是不是存在 hash map 中,以及沒有超過次數
C++
Java
沒有留言:
張貼留言