
https://leetcode.com/problems/ransom-note/
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.
canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true
public class Solution {
    public bool CanConstruct(string ransomNote, string magazine) {
        if(magazine.Length < ransomNote.Length)
        {
            return false;
        }
        
        var ransomNotes = ransomNote.ToCharArray().ToList();
        var magazines = magazine.ToCharArray().ToList();
		
        foreach(var s in ransomNotes)
        {
            int index = magazines.IndexOf(s);
            if(index >= 0)
            {
                magazines.RemoveAt(index);
            }
            else
            {
                return false;
            }
        }
        return true;
    }
}

Runtime: 80 ms, faster than 96.46% of C# online submissions.
Memory Usage: 28.1 MB, less than 100% of C# online submissions.
Time Complexity: O(n)
Space Complextiy: O(n)
這題相對簡單喔!而且我覺得是時候該偷懶一下了
題目主要是說,有個犯人想要從雜誌 (magazine) 上剪貼他想要的字到勒索信 (ransomNote) 上
false
List<char>,這樣就可以使用 IndexOf(s) 及 RemoveAt(index)
有的話,就剪貼上去 (magazines.RemoveAt(index))
沒有 的話就 return false,因為不夠用啦~以上就是這次 LeetCode 刷題的分享啦!
如果其它人有更棒的想法及意見,請留言或寄信(t4730@yahoo.com.tw) 給我。
那我們就下回見囉 