问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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 访问。

  1. public class Program {
  2. public static void Main(string[] args) {
  3. var ransomNote = "aa";
  4. var magazine = "abdfa";
  5. var res = CanConstruct(ransomNote, magazine);
  6. Console.WriteLine(res);
  7. ransomNote = "aa";
  8. magazine = "bb";
  9. res = CanConstruct2(ransomNote, magazine);
  10. Console.WriteLine(res);
  11. ransomNote = "bjaajgea";
  12. magazine = "affhiiicabhbdchbidghccijjbfj";
  13. res = CanConstruct3(ransomNote, magazine);
  14. Console.WriteLine(res);
  15. ransomNote = "edhi";
  16. magazine = "fhjeddgggbajhidhjchiedhdibgeaecffbbbefiabjdhggihccec";
  17. res = CanConstruct4(ransomNote, magazine);
  18. Console.WriteLine(res);
  19. Console.ReadKey();
  20. }
  21. private static bool CanConstruct(string ransomNote, string magazine) {
  22. //LeetCode超出内存限制未AC
  23. var startIndex = -1;
  24. for(var i = 0; i < ransomNote.Length; i++) {
  25. if(magazine.Contains(ransomNote[i])) {
  26. startIndex = magazine.IndexOf(ransomNote[i]);
  27. magazine = magazine.Remove(startIndex, 1);
  28. } else {
  29. return false;
  30. }
  31. }
  32. return true;
  33. }
  34. private static bool CanConstruct2(string ransomNote, string magazine) {
  35. //这个解法没AC,输入 aa bb 在VS下返回false
  36. //但在LeetCode上显示返回 true
  37. //不知道为什么,有看官知道告诉我一下,多谢
  38. //总之这个解可能有问题,各位看官请注意!!!
  39. var i = 0;
  40. var j = 0;
  41. if(ransomNote.Length == 1 && !magazine.Contains(ransomNote))
  42. return false;
  43. while(i < ransomNote.Length) {
  44. var temp = ransomNote[i];
  45. while(j < magazine.Length) {
  46. if(magazine[j] != temp) j++;
  47. else {
  48. i++;
  49. break;
  50. }
  51. }
  52. if(i == ransomNote.Length - 1) return true;
  53. if(j >= magazine.Length) return false;
  54. j++;
  55. }
  56. return true;
  57. }
  58. private static bool CanConstruct3(string ransomNote, string magazine) {
  59. //哈希法
  60. var dic = new Dictionary<char, int>();
  61. foreach(var c in ransomNote) {
  62. if(dic.ContainsKey(c)) {
  63. dic[c]++;
  64. } else {
  65. dic[c] = 1;
  66. }
  67. }
  68. foreach(var c in magazine) {
  69. if(dic.ContainsKey(c)) {
  70. dic[c]--;
  71. if(dic[c] < 0) return false;
  72. if(dic[c] == 0) {
  73. dic.Remove(c);
  74. }
  75. }
  76. }
  77. return dic.Count == 0;
  78. }
  79. private static bool CanConstruct4(string ransomNote, string magazine) {
  80. //用整型数组来统计26个字母
  81. //位置 0 代表字母 a,以此类推
  82. var statistics = new int[26];
  83. foreach(var c in magazine) {
  84. statistics[c - 'a']++;
  85. }
  86. foreach(var c in ransomNote) {
  87. if(--statistics[c - 'a'] < 0) return false;
  88. }
  89. return true;
  90. }
  91. }

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

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

  1. True
  2. False
  3. False
  4. 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. 关于在JSP页面识别不了EL表达式的情况

    今天在JSP页面接收Controller返回的数据user_nickname,使用EL表达式显示数据发现在页面输出的始终是字符串${user_nickname} 经过查阅资料,问题在于使用的web.x ...

  2. Go Pentester - HTTP Servers(1)

    HTTP Server Basics Use net/http package and useful third-party packages by building simple servers. ...

  3. P5198 [USACO19JAN]Icy Perimeter S (洛谷) (水搜索)

    同样是因为洛谷作业不会写…… 写(水)博客啦. 直接放题目吧,感觉放在代码框里好看点 Farmer John要开始他的冰激凌生意了!他制造了一台可以生产冰激凌球的机器,然而不幸的是形状不太规则,所以他 ...

  4. 修改虚拟机中的centos系统分辨率

    使用vmware虚拟机安装centos系统,默认分辨都很低,可使用以下方法修改虚拟机中centos系统的分辨率 1,# vi /boot/grub/grub.conf 2,找到 kernel 的那一行 ...

  5. 01 安装Linux虚拟机

    平常的工作学习中,Linux成为了一项比不可少的需要的掌握的技能,但是大部分人又不习惯于使用Linux进行生活,所以你需要在你的Windows电脑上安装一个虚拟机,那如何安装呢?其实不难,跟着我一步步 ...

  6. element ui表格实现单选 但是单选取消会报错

    1.在el-table中添加两个事件  @selection-change="handleSelectionChange"  @current-change="choos ...

  7. Kubernetes中强制删除Pod、namespace

    Kubernetes中强制删除Pod.namespace 解决方法 可使用kubectl中的强制删除命令 # 删除POD kubectl delete pod PODNAME --force --gr ...

  8. 如何使用Istio 1.6管理多集群中的微服务?

    假如你正在一家典型的企业里工作,需要与多个团队一起工作,并为客户提供一个独立的软件,组成一个应用程序.你的团队遵循微服务架构,并拥有由多个Kubernetes集群组成的广泛基础设施. 由于微服务分布在 ...

  9. PHP preg_grep() 函数

    preg_grep 函数用于返回匹配模式的数组条目.高佣联盟 www.cgewang.com 语法 array preg_grep ( string $pattern , array $input [ ...

  10. 2020牛客暑期多校训练营 第二场 C Cover the Tree 构造 贪心

    LINK:Cover the Tree 最受挫的是这道题,以为很简单 当时什么都想不清楚. 先胡了一个树的直径乱搞的贪心 一直过不去.后来意识到这类似于最经典长链剖分优化贪心的做法 然后那个是求最大值 ...