1、题目

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

2、代码实现

public class Solution {public  boolean canConstruct(String ransomNote, String magazine) {if (magazine == null)return false;if (ransomNote == null)return false;if (ransomNote.length() == 0 && magazine.length() == 0) return true;List<Character> list = new ArrayList<Character>();for (char c : magazine.toCharArray()) {list.add(Character.valueOf(c));}if (ransomNote.length() == magazine.length()) {for (int i = 0; i < ransomNote.length(); i++) {if (!list.remove(Character.valueOf(ransomNote.charAt(i)))) {return false;}}return true;} else {for (int i = 0; i < ransomNote.length(); i++) {if (!list.remove(Character.valueOf(ransomNote.charAt(i)))) {return false;}}if (list.size() > 0) {return true;}}return false;}
}

LeetCode之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(赎金票据)

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

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

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

  5. LeetCode:383. Ransom Note

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

  6. [LeetCode] NO.383 Ransom Note

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

  7. Ransom Note

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

  8. LeetCode383. Ransom Note

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

  9. LeetCode Ransom Note(字符串)

    题意:给出两个字符串s1,s2 ,看s1是否可以由s2中的字符组成 具体代码如下: public class Solution {public boolean canConstruct(String ...

最新文章

  1. 第一章 基础设施,1.3 阿里视频云ApsaraVideo是怎样让4000万人同时狂欢的(作者:蔡华)...
  2. Windows10选择文件打开方式没有始终允许的解决方案
  3. django项目开发1:搭建虚拟环境
  4. django 学习 (三) 模板变量
  5. create-react-app第一步
  6. Spring Cloud(二):服务注册与发现 Eureka【Finchley 版】
  7. lenneth -- 基于koa2 的web极简框架
  8. PS2022新增功能简介
  9. 用单片机控制直流电机
  10. 用python的tkinter包设计一个随机点菜器
  11. 高德离线地图vue-amap的api文档(1):创建地图,撒点等等
  12. 确实有必要好好学英语
  13. GBase 8s 产品简介
  14. liunx下rdesktop无法使用,安装remmina
  15. Legolas工业自动化平台案例 —— 水源地自动化监控系统
  16. C++课后习题(一)
  17. JIra+Python+Pyechart 通过分析jira数据生成图表并展示,出具质量可视化的测试报告
  18. 阿里 菜鸟网络(一面)
  19. 前端招商银行一面面经
  20. C语言详解系列——综合练习,猜数字小游戏

热门文章

  1. 巧用ActionFilterAttribute实现API日志的记录
  2. 站在巨人的肩膀,2020我在使用和涉及到的开源项目
  3. 如何在 Asp.Net Core 中 管理敏感数据
  4. Docker:恢复对开源项目的无限制访问
  5. [Mvp.Blazor] 动态路由与钩子函数
  6. ASP.NET Core 反向代理部署知多少
  7. 【译】gRPC vs HTTP APIs
  8. Abp v0.18.0 新版本: MVC Module 启动模板
  9. 基于.NET Standard的分布式自增ID算法--美团点评LeafSegment
  10. asp.net core结合Gitlab-CI实现自动化部署