[LeetCode] NO.383 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.
You may assume that both strings contain only lowercase letters.
canConstruct("a", "b") -> false canConstruct("aa", "ab") -> false canConstruct("aa", "aab") -> true
[题目解析] 首先明确题意,两个字符串,第一个字符串中的字母都是从第二个字符串中取得的。可以使用hashmap,将第一个字符串读取入hashmap,key是字符,value是对应该字符的个数。
然后遍历第二个字符串,对hashmap进行操作,依次对hashmap中含有的字符个数进行--,如果个数<0,则说明map中(即第一个字符中)含有的字符,在第二个字符串中没有覆盖到。代码如下。
public boolean canConstruct(String ransomNote, String magazine) {Map charmap = new HashMap<Character,Integer>();for(int index = 0; index < magazine.length(); index++){char tmp = magazine.charAt(index);if(charmap.containsKey(tmp)){int count = (int) charmap.get(tmp);count++;charmap.put(tmp, count);}else{charmap.put(tmp, 1);}}for(int index = 0; index < ransomNote.length(); index++){char tmp = ransomNote.charAt(index);if(charmap.containsKey(tmp)){int count = (int) charmap.get(tmp);count--;if(count < 0) return false;charmap.put(tmp, count);}else{return false;}}return true; }
考虑到字符串中只有小写字母,我们可以优化下代码,整体思路不变。代码如下。
public boolean canConstruct(String ransomNote, String magazine) {int[] arr = new int[26];for (int i = 0; i < magazine.length(); i++) {arr[magazine.charAt(i) - 'a']++;}for (int i = 0; i < ransomNote.length(); i++) {if(--arr[ransomNote.charAt(i)-'a'] < 0) {return false;}}return true;}
转载于:https://www.cnblogs.com/zzchit/p/5770355.html
[LeetCode] NO.383 Ransom Note相关推荐
- LeetCode:383. Ransom Note
051104 题目 Given an arbitrary ransom note string and another string containing letters from all the m ...
- Leetcode 383 Ransom Note
lc383 Ransom Note 两个for 第一个记录sourse字符串每种字母出现次数 第二个看现有字母是否能够填满target 1 class Solution { 2 public bool ...
- LeetCode 383. Ransom Note
题目 : Given an arbitrary ransom note string and another string containing letters from all the magazi ...
- leetcode 383. Ransom Note(赎金票据)
Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the ...
- 383. Ransom Note/691. Stickers to Spell Word-- String, Map, back tracking-- 未完待续
383 easy 题,就是建立字母的hash 表 看第一个String 是否能被第二个String 所构建 canConstruct("aa", "aab") ...
- C#LeetCode刷题之#383-赎金信(Ransom Note)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3937 访问. 给定一个赎金信 (ransom) 字符串和一个杂志 ...
- LeetCode之Ransom Note
1.题目 Given an arbitrary ransom note string and another string containing letters from all the magazi ...
- Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the ...
- LeetCode383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the ...
最新文章
- Network 之一 国际标准组织介绍、互联网/因特网、以太网概念区分、协议标准
- ai/ml_您本周应阅读的有趣的AI / ML文章(8月9日)
- abstract类中可以有private的成员_C++|static成员与单例模式
- Python xlwt : More than 4094 XFs (styles) 解决方法
- 51nod-1337:翻转游戏
- 7000个源码批量下载---复制来的
- orcale :SQL语句小测试select * from emp order by hiredate asc;
- 工业物联网:平台架构、关键技术与应用实践
- Android软键盘适配问题
- HTML5期末大作业:旅游网站设计——中国风的旅游网站(9页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 计算机毕设网页设计源码...
- u盘损坏后如何恢复数据?
- 如何查看计算机硬盘的软件内存条,内存检测工具,内存检测软件 - 内存条检测工具就用金山卫士 - 安全专题...
- BART 文本摘要示例
- android 渐变歌词,Android仿酷狗动感歌词(支持翻译和音译歌词)显示效果
- 使用RestTemplate请求第三方接口出错,没抛出异常?
- 在Java中获取两个数的中间值(超大数值)
- [ARM+Linux] 基于全志h616外设开发笔记
- 【NOIP2014模拟11.2A组】福慧双修
- 微信用户免密免验证码登录
- 王者荣耀小游戏(发给你的好兄弟(大怨种)^-^)