题目:

比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 大写字母

样例

给出 A = "ABCD" B = "ACD",返回 true

给出 A = "ABCD" B = "AABC", 返回 false

注意

在 A 中出现的 B 字符串里的字符不需要连续或者有序。

解题:

用数组,或者用HashMap都可以,由于char String转换成Integer不是很熟悉,搞了好久。。。

Java程序:

public class Solution {
/**
* @param A : A string includes Upper Case letters
* @param B : A string includes Upper Case letter
* @return : if string A contains all of the characters in B return true else return false
*/
public boolean compareStrings(String A, String B) {
// write your code here
if(A.length()<B.length())
return false;
if(B==null||B.length()==0)
return true;
int array[] = new int[26];
char ch;
for(int i = 0;i< A.length();i++){
ch = A.charAt(i);
array[ ch - 'A' ] ++;
}
for(int i = 0;i< B.length();i++){
ch = B.charAt(i);
array[ ch - 'A' ] --;
if(array[ ch - 'A' ] < 0)
return false;
}
return true;
}
}

总耗时: 2110 ms

通过Integer.parseInt(Str)会有异常,可以设置默认值,但是我抛出或者使用try catch 我都没有成功。。。。。下面Python利用字典实现

Python程序:

class Solution:
"""
@param A : A string includes Upper Case letters
@param B : A string includes Upper Case letters
@return : if string A contains all of the characters in B return True else return False
"""
def compareStrings(self, A, B):
# write your code here
if len(A)<len(B):
return False
if len(B)==0 or B==None:
return True
d = {}
for ai in A:
if ai not in d:
d[ai] = 1
else:
d[ai] += 1
for bi in B:
if bi not in d:
return False
else:
d[bi] -=1
if d[bi] < 0:
return False
return True

总耗时: 704 ms

更新

KMP算法可以尝试解题

lintcode:Compare Strings 比较字符串的更多相关文章

  1. LintCode 58: Compare Strings

    LintCode 58: Compare Strings 题目描述 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是大写字母. 样例 给出A = "ABCD&q ...

  2. 【Leetcode_easy】1170. Compare Strings by Frequency of the Smallest Character

    problem 1170. Compare Strings by Frequency of the Smallest Character 参考 1. Leetcode_easy_1170. Compa ...

  3. LeetCode.1170-比较字符串中最小字符的出现频率(Compare Strings by Frequency of the Smallest Char)

    这是小川的第412次更新,第444篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第263题(顺位题号是1170).在一个非空字符串s上定义一个函数f(s),该函数计算s中最小字 ...

  4. codeforces 112APetya and Strings(字符串水题)

    A. Petya and Strings 点击打开题目 time limit per test 2 seconds memory limit per test 256 megabytes input ...

  5. [LeetCode] Compare Version Numbers 字符串操作

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  6. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  7. Compare Strings

    Compare two strings A and B, determine whether A contains all of the characters in B. The characters ...

  8. Equivalent Strings (字符串相等?)

    Equivalent Strings   E - 暴力求解.DFS Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I ...

  9. leetcode:Multiply Strings(字符串的乘法)【面试算法题】

    题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...

随机推荐

  1. jquery 从页面获取li数组,删除不在数组中的key

    应用场景: 获取页面 li 下面 key的值,添加到 arr数组 删除车型不在arr 数组中的value值. 示例代码: var getSaleModels = function(brand_id){ ...

  2. 借Windows说明Linux分区和挂载点[转]

    在介绍Linux分区和挂载点前,我想先说一个Windows的例子,Windows大家都比较熟,再借这个例子来说明什么是Linux分区和挂载点. 1.消失了的分区 在WinPE下,我将一块硬盘分成一个主 ...

  3. 七天学会NodeJS-学习笔记

    在网上发现一篇nodeJS教程,名为七天学会NodeJS,标题很有吸引力.我不指望七天能学会,只希望可以入门,下面是我的学习笔记和遇到的问题. 教程网址:http://nqdeng.github.io ...

  4. Lucene 3.0

    http://www.cnblogs.com/forfuture1978/archive/2010/02/22/1671487.html http://www.cnblogs.com/jiekzou/ ...

  5. Linux系统下sendmail发送邮件失败的问题

         问题是:安装完sendmail,启动服务后,发送邮件第一次发送成功,后面再次无论怎么发送都不行,换邮箱也不行.在确认我的邮件发送格式正确无误后,想到查看邮件发送日志: [root@backu ...

  6. JMS概述

    [1.面向消息的中间件]顾名思义,面向消息的中间件就是通过使用消息(而不是命令)将企业内的组件连接起来的系统.例如库存系统可能会与工资和会计系统进行通信,如果使用面向消息的中间件将他们连接在一起,就可 ...

  7. Storm集群安装详解

    storm有两种操作模式: 本地模式和远程模式. 本地模式:你可以在你的本地机器上开发测试你的topology, 一切都在你的本地机器上模拟出来; 远端模式:你提交的topology会在一个集群的机器 ...

  8. MySQL监控工具-orztop

    先安装orzdba,链接:http://blog.itpub.net/28939273/viewspace-1875895/ 安装依赖的包:[root@hank-yoon servers]# yum ...

  9. MySQL监控工具-orzdba

    源代码地址:http://code.taobao.org/p/orzdba/src/trunk/     [root@hank-yoon servers]# chmod +x orzdba 在代码的1 ...

  10. MySQL创建复合索引

    在MySQL数据库中,创建复合索引的时候,不知道在创建过程中哪个列在前面,哪个列该在后面,用以下方式即可: select count(distinct first_name)/count(*) as ...