问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3937 访问。

给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成。如果可以构成,返回 true ;否则返回 false。

(题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。)

注意:你可以假设两个字符串均只含有小写字母。

canConstruct("a", "b") -> false

canConstruct("aa", "ab") -> false

canConstruct("aa", "aab") -> true


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


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3937 访问。

public class Program {

    public static void Main(string[] args) {
var ransomNote = "aa";
var magazine = "abdfa"; var res = CanConstruct(ransomNote, magazine);
Console.WriteLine(res); ransomNote = "aa";
magazine = "bb"; res = CanConstruct2(ransomNote, magazine);
Console.WriteLine(res); ransomNote = "bjaajgea";
magazine = "affhiiicabhbdchbidghccijjbfj"; res = CanConstruct3(ransomNote, magazine);
Console.WriteLine(res); ransomNote = "edhi";
magazine = "fhjeddgggbajhidhjchiedhdibgeaecffbbbefiabjdhggihccec"; res = CanConstruct4(ransomNote, magazine);
Console.WriteLine(res); Console.ReadKey();
} private static bool CanConstruct(string ransomNote, string magazine) {
//LeetCode超出内存限制未AC
var startIndex = -1;
for(var i = 0; i < ransomNote.Length; i++) {
if(magazine.Contains(ransomNote[i])) {
startIndex = magazine.IndexOf(ransomNote[i]);
magazine = magazine.Remove(startIndex, 1);
} else {
return false;
}
}
return true;
} private static bool CanConstruct2(string ransomNote, string magazine) {
//这个解法没AC,输入 aa bb 在VS下返回false
//但在LeetCode上显示返回 true
//不知道为什么,有看官知道告诉我一下,多谢
//总之这个解可能有问题,各位看官请注意!!!
var i = 0;
var j = 0;
if(ransomNote.Length == 1 && !magazine.Contains(ransomNote))
return false;
while(i < ransomNote.Length) {
var temp = ransomNote[i];
while(j < magazine.Length) {
if(magazine[j] != temp) j++;
else {
i++;
break;
}
}
if(i == ransomNote.Length - 1) return true;
if(j >= magazine.Length) return false;
j++;
}
return true;
} private static bool CanConstruct3(string ransomNote, string magazine) {
//哈希法
var dic = new Dictionary<char, int>();
foreach(var c in ransomNote) {
if(dic.ContainsKey(c)) {
dic[c]++;
} else {
dic[c] = 1;
}
}
foreach(var c in magazine) {
if(dic.ContainsKey(c)) {
dic[c]--;
if(dic[c] < 0) return false;
if(dic[c] == 0) {
dic.Remove(c);
}
}
}
return dic.Count == 0;
} private static bool CanConstruct4(string ransomNote, string magazine) {
//用整型数组来统计26个字母
//位置 0 代表字母 a,以此类推
var statistics = new int[26];
foreach(var c in magazine) {
statistics[c - 'a']++;
}
foreach(var c in ransomNote) {
if(--statistics[c - 'a'] < 0) return false;
}
return true;
} }

以上给出4种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3937 访问。

True
False
False
True

分析:

显而易见,以上4种算法的时间复杂度均为:  。

C#LeetCode刷题之#383-赎金信(Ransom Note)的更多相关文章

  1. [Swift]LeetCode383. 赎金信 | Ransom Note

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

  2. C#LeetCode刷题-字符串

    字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.6% 中等 5 最长回文子串   22.4% 中等 6 Z字形变换   35.8% 中等 8 字符串转整数 (atoi)   ...

  3. leetcode.383赎金信

    给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成.如果可以构成,返回 true :否则返回 ...

  4. Java实现 LeetCode 383 赎金信

    383. 赎金信 给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成.如果可以构成,返回 t ...

  5. 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和

    第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...

  6. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  7. leetcode 刷题进展

    最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多  前200的吃透了 足以应付非算法岗 ...

  8. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  9. leetcode刷题记录--js

    leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...

随机推荐

  1. Ubuntu虚拟机设置共享文件夹

    Ubuntu虚拟机设置共享文件夹 注:个人笔记,小白笔记. 点击设置 进入到Ubuntu 中 cd /mnt/hgfs/ 后   我们会看到自己设置的共享文件夹.

  2. Go Pentester - HTTP CLIENTS(2)

    Building an HTTP Client That Interacts with Shodan Shadon(URL:https://www.shodan.io/)  is the world' ...

  3. Ethical Hacking - GAINING ACCESS(11)

    CLIENT SIDE ATTACKS - Listening for connections 1. Run Metasploit Move the backdoor file to the webs ...

  4. OSCP Learning Notes - Buffer Overflows(5)

    Generating Shellcode & Gaining Root 1.Generate the shellcode on Kali Linux. LHOST is the IP of K ...

  5. centos 构建dns服务 dnsmasq

    1 安装yum -y install dnsmasq开放udp tcp 53 端口2,修改配置文件 dnsmasq.conf# grep -Ev "^$|^[#;]" /etc/d ...

  6. 题解 CF13E 【Holes】

    这个题和$P3203\ $弹飞绵羊基本上完全一致 我的做法是用\(LCT\)维护信息,开一个节点\(fly\),表示到此节点时,小球会弹飞,那么查询弹多少次即为\(siz[fly]-1\) 最后一次落 ...

  7. Python 编程语言的核心是什么?

    01 Python 编程语言的核心是什么? ​   为什么要问这个问题? 我想要用Python实现WebAssembly,这并不是什么秘密.这不仅可以让Python进入浏览器,而且由于iOS和Andr ...

  8. APP自动化 -- 滑动解锁、滑动验证

    一.解锁 1.代码 2.效果 1)执行效果 2)点位效果

  9. BUUCTF-web EasySearch (服务端包含注入ssi)

    一打开就是登录页面 存在index.php.swp...(反正我是没有扫出来,题目没给提示),分析一波源码 <?php ob_start(); function get_hash(){ $cha ...

  10. 跟老刘学运维day02~新手必须掌握的Linux命令(2)

    第2章 Linux命令 1.Shell 计算机硬件:由运算器.控制器.存储器.输入/输出设备等共同组成 Shell:人与硬件的翻译官,人要想使用硬件,需要服务程序 Bash四大好处: (1)通过上下方 ...