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
代码如下:
 public class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
if(ransomNote.length()<=magazine.length())
{
int i=0;
while(i<ransomNote.length())
{
int index=magazine.indexOf(ransomNote.charAt(i));
if(index!=-1)
{
magazine= magazine.replaceFirst(magazine.substring(index,index+1),"");
i++;
}
else return false;
}
}
else return false; return true;
}
}

383. Ransom Note的更多相关文章

  1. 383. Ransom Note【easy】

    383. Ransom Note[easy] Given an arbitrary ransom note string and another string containing letters f ...

  2. leetcode 383. Ransom Note

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

  3. leetcode修炼之路——383. Ransom Note

    题目是这样的 Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 a ...

  4. 14. leetcode 383. Ransom Note

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

  5. [LeetCode&Python] Problem 383. Ransom Note

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

  6. Java [Leetcode 383]Ransom Note

    题目描述: Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 al ...

  7. 383. Ransom Note 在字典数组中查找笔记数组

    [抄题]: Given an arbitrary ransom note string and another string containing letters from all the magaz ...

  8. LeetCode: 383 Ransom Note(easy)

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

  9. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

随机推荐

  1. c++ 普通高精乘

    //第一次提交全错了,太过相信自己了. //给我教训是注意循环后变量的值,和pascal不一样. //就不贴错误代码了. //codevs3118 高精度练习之除法 #include<cstdi ...

  2. jQuery 中的 Ajax

    jQuery 对 Ajax 操作进行了封装, 在 jQuery 中最底层的方法时 $.ajax(), 第二层是 load(), $.get() 和 $.post(), 第三层是 $.getScript ...

  3. struts2的返回类型

    return 一个字符串,如果是success 直接 服务器端跳转 返回到和方法名对应的页面去 不过如果返回的页面和方法没有太大关系,比如删除修改添加之后要 客户端跳转 返回所有用户列表,这个时候可以 ...

  4. 《day17_String_StringBuffer》

    package cn.itcast.api.string; public class StringDemo{ public static void main(String[] args){ //定义一 ...

  5. 关于$.getJson

    这是一个Ajax函数的缩写,这相当于: 1 2 3 4 5 6 $.ajax({ dataType: "json", url: url, data: data, success: ...

  6. IIS 发布后文件拒绝访问

    今天遇到一个很小的问题,代码中写XML文件,本地运行没有问题,一发布到服务器上就出现 代码如下: XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load ...

  7. poj1458

    //Accepted 4112 KB 16 ms //最长公共子串 #include <cstdio> #include <cstring> #include <iost ...

  8. ios上 更改 状态栏(UIStatusBar)

    摘要 ios上 更改状态栏(UIStatusBar)的颜色 ios UIStatusBar statusBar 状态栏 更改状态栏颜色 目录[-] IOS上 关于状态栏的相关设置(UIStatusBa ...

  9. php中数据库的操作

    1.Mysql客户端介绍,命令行:这种方法不友好. 2.Mysql客户端介绍,Web形式的可视化界面(phpMyAdmin) 优点:只要有浏览器就可以操作数据库 缺点: a)创建数据库

  10. linux命令:pwd

    1.介绍: pwd可以查看目录的完整路径,全称是Print Working Directory,. 2.命令格式 pwd [选项] 3.命令作用 查看目录的完整路径 4.常用选项 -L 当路径为链接路 ...