Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.

Each letter in magazine can only be used once in ransomNote.

Example 1:

Input: ransomNote = “a”, magazine = “b”
Output: false
Example 2:

Input: ransomNote = “aa”, magazine = “ab”
Output: false
Example 3:

Input: ransomNote = “aa”, magazine = “aab”
Output: true

题目是赎金,但是好像和赎金也没啥关系,就是字符串。
ransomNote中的字母要由magazine中的字母组成。
magazine中每个字母只能用一次。

思路:

说白了就是判断ransomNote是不是magazine的子串,即由magazine中的字母组成,不限制顺序。
但是magazine中每个字母只能用一次。

英文小写字母总共就26个,只要统计每个字母出现的次数,
ransomNote中每个字母出现的次数只要小于magazine中对应字母的次数,就为true.

public boolean canConstruct(String ransomNote, String magazine) {int nr = ransomNote.length();int nm = magazine.length();int n = Math.max(nr, nm);int[] cntr = new int[26];int[] cntm = new int[26];for(int i = 0; i < n; i ++) {if(i < nr) cntr[ransomNote.charAt(i)-'a'] ++;if(i < nm) cntm[magazine.charAt(i)-'a'] ++;}for(int i = 0; i < 26; i++) {if(cntr[i] > cntm[i]) return false;}return true;
}

还有一种方法,不统计次数,直接依次查找,找不到就返回false.
由于magazine中每个字母只能用一次,一旦这个字母用过就不能再用,假设这个字母的index为 i,
下次再找这个字母,只能从i + 1开始找。

保存每个字母当前找到的index, 下次从index + 1开始找这个字母。

public boolean canConstruct(String ransomNote, String magazine) {int[] startIdx = new int[26];for(char cur : ransomNote.toCharArray()) {int index = magazine.indexOf(cur, startIdx[cur - 'a']);if(index == -1) return false;startIdx[cur-'a'] = index+1;}return true;
}

leetcode 383. Ransom Note(赎金票据)相关推荐

  1. Leetcode 383 Ransom Note

    lc383 Ransom Note 两个for 第一个记录sourse字符串每种字母出现次数 第二个看现有字母是否能够填满target 1 class Solution { 2 public bool ...

  2. LeetCode 383. Ransom Note

    题目 : Given an arbitrary ransom note string and another string containing letters from all the magazi ...

  3. LeetCode:383. Ransom Note

    051104 题目 Given an arbitrary ransom note string and another string containing letters from all the m ...

  4. [LeetCode] NO.383 Ransom Note

    [题目] Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all ...

  5. LeetCode之Ransom Note

    1.题目 Given an arbitrary ransom note string and another string containing letters from all the magazi ...

  6. 383. Ransom Note/691. Stickers to Spell Word-- String, Map, back tracking-- 未完待续

    383  easy 题,就是建立字母的hash 表 看第一个String 是否能被第二个String 所构建 canConstruct("aa", "aab") ...

  7. C#LeetCode刷题之#383-赎金信(Ransom Note)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3937 访问. 给定一个赎金信 (ransom) 字符串和一个杂志 ...

  8. LeetCode.383. 赎金信

    LeetCode.383. 赎金信 难度:easy 利用数组来做哈希表,分别统计两个字符串中各个字母的出现次数: class Solution {public boolean canConstruct ...

  9. Ransom Note

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

最新文章

  1. java程序ssh置顶_使用shell脚本启动远程(SSH)Java应用程序不会返回本地提示
  2. LED音乐频谱之输出数据处理
  3. Android 事件分发面试题2
  4. CMMI入门-通用目标的实施- GG3
  5. python推荐淘宝物美价廉商品 2.0
  6. [bzoj 1954]Pku3764 The xor-longest Path
  7. jenkins 下载插件 一直失败_Jenkins安装与插件下载
  8. XSL样式,分页方法
  9. 如何在Mac电脑上更改地区或国家位置设定?
  10. FreeFileSync 免费文件同步软件 实时自动备份重要资料
  11. 分销系统如何加粉,三级分销政策,B2B2C分销规则
  12. appbase_使用Appbase.io和JavaScript构建类似于Live Search Feed的Twitter
  13. Wangle源码分析:Pipeline、Handler、Context
  14. 【产品经理】 产品经理进阶之路(十一):怎么看微信的公众号和百度的直达号,哪个更有优势
  15. Lesson 13-14 How often do you exercise?
  16. 韦东山第1期-学习笔记-1
  17. 13-津巴布韦-杂项
  18. win10虚拟内存怎么设置最好_win10系统如何设置虚拟内存,看这里!
  19. 傻瓜交换机带来的问题
  20. Bench 2022 | 第一届 OpenBench 国际研讨会精彩回顾

热门文章

  1. Linux环境下应用生成图片中出现乱码的问题处理
  2. python制作学生管理系统_Python——面向对象,简易学生信息管理系统
  3. ios 实时全景拼图
  4. javase个人小结
  5. 专业导论-课后总结-2019.12.05-V1.0
  6. C++Qt开发——音视频播放
  7. 使用Golang搭建gRPC服务提供给.NetCore调用
  8. 循环赛赛程表的分治法实现
  9. Linux中常用工具包的安装与下载及vim的使用
  10. Android5.0及以上版本新特性