383. Ransom Note
来源:互联网 发布:旗鱼网络加速器 编辑:程序博客网 时间:2024/06/06 21:53
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") -> falsecanConstruct("aa", "ab") -> falsecanConstruct("aa", "aab") -> true
思路:
这是LeetCode Discuss中的最热代码,它的原理就是列出了magazine的字母表,然后算出了出现个数,然后遍历ransomNote,保证有足够的字母可用,代码非常清晰。
class Solution { 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; }}
阅读全文
0 0
- leetcode-383. Ransom Note
- [leetcode] 383. Ransom Note
- LeetCode 383. Ransom Note
- 383. Ransom Note*
- 383. Ransom Note
- leetcode 383. Ransom Note
- leetcode 383. Ransom Note
- 383. Ransom Note
- 383.[LeetCode]Ransom Note
- 383. Ransom Note
- 383. Ransom Note【E】
- leetcode 383. Ransom Note
- 383. Ransom Note
- 383. Ransom Note
- leetcode 383. Ransom Note
- Leetcode 383. Ransom Note
- 【leetcode】383. Ransom Note
- Leetcode 383. Ransom Note
- Programming Entity Framework DbContext.pdf 英文原版 免费下载
- MVP
- 如何用命令删除find出来的文件
- 哈尔滨理工大学第七届程序设计竞赛决赛(网络赛-低年级组)
- VSFTP服务搭建及FTP并发的核心理解
- 383. Ransom Note
- 【Scikit-Learn 中文文档】监督学习:从高维观察预测输出变量
- Linux DNS服务器配置
- python_sys_module
- Android把字符串中的数字变为红色
- C# 5 First Look.pdf 英文原版 免费下载
- 选择结构的嵌套
- C++做一个返回数组指针的函数
- 下拉刷新的小控件