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,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
随机推荐
- 使用 JS 开发 Github Actions 实现自动部署前后台项目到自己服务器
不想看前面这么多废话的可以直接跳到具体实现 Github Actions 是什么? 说到 Github Actions 不得不提一下. 持续集成(continuous integration):高质量 ...
- MSF查找提权exp
0x01:介绍 在拿到一个反弹shell后,下一步可以用metaspolit的内置模块Local Exploit SuggesterLocal-exploit-suggester的功能就如它的名字一样 ...
- 30个Linux Shell脚本经典案例(上)
编写Shell过程中注意事项: 开头加解释器:#!/bin/bash 语法缩进,使用四个空格:多加注释说明. 命名建议规则:变量名大写.局部变量小写,函数名小写,名字体现出实际作用. 默认变量是全局的 ...
- 图灵学院笔记-java虚拟机底层原理
Table of Contents generated with DocToc 一.java虚拟机概述 二.栈内存解析 2.1 概述 2.2 栈帧内部结构 2.2.1 我们来解析一下compute() ...
- C++语法小记---多重继承
多重继承 工程中不建议使用多继承,因为多继承带来的问题比带来的便利多,已被放弃 问题一:多重继承的对象,向上获取指针时,有不同的地址 ----无法解决 问题二:菱形继承问题,导致成员冗余 ----虚继 ...
- 脸书(Facebook)如何绑定谷歌二次验证码/谷歌身份验证/双重认证?
1.打开Facebook,找到双重验证界面 打开Facebook,点击“设置”-“安全与登陆”-“使用双重验证”-“身份验证应用”-“在其他设备上设置应用”-“输入验证码” *****想使用Fac ...
- WinForm微信扫码登录
源码还需优化,不喜勿喷. 微信官方文档 : https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat ...
- windy数(数位dp)
https://www.luogu.com.cn/blog/virus2017/shuweidp https://www.luogu.com.cn/problem/P2657 #include < ...
- 一年经验Java开发0713面试
@ 目录 介绍一下你做的某些模块,有些什么比较复杂的地方? 你们的文件怎么存储的? 怎么没有用文件服务器? 文件存储有没有做备份? 在项目上有没有什么搞不定的问题? 对搞不定的问题你是怎么处理的? 你 ...
- MySQL组复制MGR(四)-- 单主模式与多主模式
(一)概述 组复制可以运行在单主模式下,也可以运行在多主模式下,默认为单主模式.组的不同成员不能部署在不同模式下,要切换模式,需要使用不同配置重新启动组而不是单个server. 相关参数如下: # 该 ...