lintcode:Compare Strings 比较字符串
题目:
比较两个字符串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 比较字符串的更多相关文章
- LintCode 58: Compare Strings
LintCode 58: Compare Strings 题目描述 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是大写字母. 样例 给出A = "ABCD&q ...
- 【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 ...
- LeetCode.1170-比较字符串中最小字符的出现频率(Compare Strings by Frequency of the Smallest Char)
这是小川的第412次更新,第444篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第263题(顺位题号是1170).在一个非空字符串s上定义一个函数f(s),该函数计算s中最小字 ...
- codeforces 112APetya and Strings(字符串水题)
A. Petya and Strings 点击打开题目 time limit per test 2 seconds memory limit per test 256 megabytes input ...
- [LeetCode] Compare Version Numbers 字符串操作
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Compare Strings
Compare two strings A and B, determine whether A contains all of the characters in B. The characters ...
- Equivalent Strings (字符串相等?)
Equivalent Strings E - 暴力求解.DFS Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I ...
- leetcode:Multiply Strings(字符串的乘法)【面试算法题】
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
随机推荐
- NSS_11 Server Error in '/' Application
昨天手贱在Home/Index下点了下鼠标,set as start page,然后程序一直运行不起来, 一度以为mvc的route失效了, 一直报一个错误如下:
- NSS_07 extjs中grid在工具条上的查询
碰到的每个问题, 我都会记下走过的弯路,尽量回忆白天的开发过程, 尽量完整, 以使自己以后可以避开这些弯路. 这个问题在系统中应用得比较多, 在一个gridpanel的工具条上有俩搜索框, panel ...
- 【转】理解JavaScript之闭包
闭包(closure)是掌握Javascript从人门到深入一个非常重要的门槛,它是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现.下面写下我的学习笔记~ 闭包-无处不 ...
- 【PHP】phpcms html去除空白
// 文件路径:/phpcms/libs/classes/template_cache.class.php 42行 // 第四第五行是新增的 $content = $this->template ...
- DOS命令之----Netstat+Task以及相关使用
作为一个初步接触电脑的人,在学习Android的过程中,遇到各种问题,今天遇到了.这样一个错误提示: The connection to adb is down, and a severe error ...
- yabeblog.me 关于Tomcat7部署 一台机器部署两个项目,一个用域名访问,一个用IP访问
该内容来自 http://www.yabeBlog.me,转载请说明出处. 1.使用IP访问的项目放在Tomcat7 的webapps目录下面:比如:AAA 2.使用域名访问的项目放在Tomcat7的 ...
- ASP.NET MVC +EasyUI 权限设计(三)基础模块
请注明转载地址:http://www.cnblogs.com/arhat 在上一章中呢,我们基本上搭建好了环境,那么本章我们就从基础模块开始写起.由于用户,角色,动作三个当中,都是依赖与动作的,所以本 ...
- 10.31Daily Scrum
人员 任务分配完成情况 明天任务分配 王皓南 主网页的框架搭建,任务编号752 研究代码,学习相应语言,讨论设计思路 申开亮 学习数据库的操作,任务编号753 研究代码,学习相应语言,讨论设计思路 王 ...
- 面试问到的Spring
一.介绍Spring 1.主要使用了基本的javabean代替的Ejb Ejb:服务端的组件模型,设计目标应用部署分布在应用程序,把已经做好的编好的程序,打包放在服务 端执行,凭借java跨平台的优 ...
- ORA-06550:line 1,column 7;PLS-00201:indentifer '存储过程' must be declared;...PL/SQL Statement ignored 问题
前段时间由于修改SMES系统,出现了一个问题. ORA-06550:line 1,column 7;PLS-00201:indentifer '存储过程' must be declared;...PL ...