C#LeetCode刷题之#383-赎金信(Ransom Note)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- [Swift]LeetCode383. 赎金信 | Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) ...
- leetcode.383赎金信
给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成.如果可以构成,返回 true :否则返回 ...
- Java实现 LeetCode 383 赎金信
383. 赎金信 给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成.如果可以构成,返回 t ...
- 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和
第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- leetcode 刷题进展
最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多 前200的吃透了 足以应付非算法岗 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
随机推荐
- 关于在JSP页面识别不了EL表达式的情况
今天在JSP页面接收Controller返回的数据user_nickname,使用EL表达式显示数据发现在页面输出的始终是字符串${user_nickname} 经过查阅资料,问题在于使用的web.x ...
- Go Pentester - HTTP Servers(1)
HTTP Server Basics Use net/http package and useful third-party packages by building simple servers. ...
- P5198 [USACO19JAN]Icy Perimeter S (洛谷) (水搜索)
同样是因为洛谷作业不会写…… 写(水)博客啦. 直接放题目吧,感觉放在代码框里好看点 Farmer John要开始他的冰激凌生意了!他制造了一台可以生产冰激凌球的机器,然而不幸的是形状不太规则,所以他 ...
- 修改虚拟机中的centos系统分辨率
使用vmware虚拟机安装centos系统,默认分辨都很低,可使用以下方法修改虚拟机中centos系统的分辨率 1,# vi /boot/grub/grub.conf 2,找到 kernel 的那一行 ...
- 01 安装Linux虚拟机
平常的工作学习中,Linux成为了一项比不可少的需要的掌握的技能,但是大部分人又不习惯于使用Linux进行生活,所以你需要在你的Windows电脑上安装一个虚拟机,那如何安装呢?其实不难,跟着我一步步 ...
- element ui表格实现单选 但是单选取消会报错
1.在el-table中添加两个事件 @selection-change="handleSelectionChange" @current-change="choos ...
- Kubernetes中强制删除Pod、namespace
Kubernetes中强制删除Pod.namespace 解决方法 可使用kubectl中的强制删除命令 # 删除POD kubectl delete pod PODNAME --force --gr ...
- 如何使用Istio 1.6管理多集群中的微服务?
假如你正在一家典型的企业里工作,需要与多个团队一起工作,并为客户提供一个独立的软件,组成一个应用程序.你的团队遵循微服务架构,并拥有由多个Kubernetes集群组成的广泛基础设施. 由于微服务分布在 ...
- PHP preg_grep() 函数
preg_grep 函数用于返回匹配模式的数组条目.高佣联盟 www.cgewang.com 语法 array preg_grep ( string $pattern , array $input [ ...
- 2020牛客暑期多校训练营 第二场 C Cover the Tree 构造 贪心
LINK:Cover the Tree 最受挫的是这道题,以为很简单 当时什么都想不清楚. 先胡了一个树的直径乱搞的贪心 一直过不去.后来意识到这类似于最经典长链剖分优化贪心的做法 然后那个是求最大值 ...