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 ...
随机推荐
- jQuery滚动监听插件Waypoints
页面内滚动操作的导航插件Waypoints.它可以让你方便的处理页面滚动事件,你可以比较自由的在自己的UI中使用这个插件控制页面滚动事件. Waypoints根据用户滚动的位置来帮助开发者构建相关的设 ...
- ORACLE 语句关联统计
很久不用SQL语句了,貌似入职新公司后,又回归到了三年前的SQL时代,一写一坨的SQL好吧,也当回归一下过去的知识. 下面是统计2月份某数据的计费统计 select t.telno as 主号,VID ...
- 配置Windows 2008 R2 64位 Odoo 8.0/9.0 源码开发调试环境
安装过程中,需要互联网连接下载python依赖库: 1.安装: Windows Server 2008 R2 x64标准版 2.安装: Python 2.7.10 amd64 到C:\Python27 ...
- 关于C语言指针中的p++与p+i
先看一组代码: #include <stdio.h> void main() { int i,*p,a[7]; p=a; for(i=0;i<7;i++) scanf("% ...
- postgreSQL数据库(索引、视图)
索引的含义与特点 索引是一个单独的.存储在磁盘上的数据库结构,它们包含对数据所有记录的引用指针,postgresql列类型都可以被索引,对相关列索引是提高查询操作效率的最佳途径.例如,查询select ...
- Android开发——AsyncTask详解
android提供AsynvTask,目的是为了不阻塞主线程(UI线程),且UI的更新只能在主线程中完成,因此异步处理是不可避免的. Android为了降低开发难度,提供了AsyncTask.Asyn ...
- kruskal --- C++
#include <cstdio> #include <algorithm> using namespace std; struct aaa{ int l,r,w; bool ...
- phpcms v9后台登陆验证码无法显示,怎么取消验证码
phpcms v9后台登陆验证码无法显示论坛里关于这个问题貌似一直没有解决,查看源代码后发现,关键一点是获取验证码的图片与全局变量SITE_URL相关,也就是网站的目录, 所以只要修改cache/co ...
- 计划:怎样理解水平集方法 ITK Level set V4 框架介绍
简易解释:在曲面中插入一个平面所形成的轮廓,即是该轮廓的水平集表示,可见,该轮廓的水平集表示有多个.对于图像分割,在图像力的驱动下曲面进行更新. 轮廓的数学表达有隐式和显式两种表达.用曲面演化代替Fr ...
- RabbitMQ学习(1):安装
1.安装 Erlang,官网:https://www.erlang.org/ 2.安装RabbitMQ服务器,rabbitMQ server,官网http://www.rabbitmq.com/ 注: ...