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的toggle()的自动触发真烦人
jQuery的toggle()方法应该是在鼠标点击后才会触发,现在的问题是在ready加载后就自动触发了,怎么回事呢? 答案是jQuery的版本问题,在1.9以后的版本toggle()就存在这个问题, ...
- 【Qt】Qt之自定义界面(右下角冒泡)【转】
简述 网页右下角上经常会出现一些提示性的信息,桌面软件中也比较常见,类似360新闻.QQ消息提示一样! 这种功能用动画实现起来很简单,这节我们暂时使用定时器来实现,后面章节会对动画框架进行详细讲解. ...
- 读取iis日志到sql server
using Fasterflect; using System; using System.Collections.Generic; using System.Data.SqlClient; usin ...
- [JS进阶] 编写可维护性代码 (1)
今天的web应用大至成千上万行的javascript代码,执行各种复杂的过程,这种演化让我们开发者必须得对可维护性有一定的认识!编写可维护性代码很重要,很多情况下我们是以他人的工作成果为基础,确保代码 ...
- 关于SQLite的创建以及使用相关说明
关于SQLite的创建以及使用相关说明 没有给出具体的程序,但看完这后可能对你有所帮助. 数据库操作基本知识: execSQL(String sql): 执行一个数据库语句 insert(table, ...
- 批量修改文件名(Python)
和上篇博文经历类似, 批量修改文件名字. : ) 不多说, 看图, 程序运行之前: 程序要做的事情呢, 就是挖出"[]"中的集数, 并用 “第[]集”来修改文件名字. 下面是Pyt ...
- RealThinClient (RTC)是什么?
RealThinClient SDK是用于开发标准的HTTP(S)服务器,ISAPI扩展以及客户端的VCL控件.可用于Windows下的CodeGear Delphi 6-XE5. 功能描述 Abou ...
- css透明(支持各浏览器)
opacity: 0.4;filter: progid:DXImageTransform.Microsoft.Alpha(opacity=40); -ms-filter: "progid:D ...
- sublimetext2 中运行Python提示EOFError: EOF when reading a line
解决方法:一.安装sublimeREPL 打开sublimeText2按CTRL+SHIFT+P,英文版输入:install后选择Package Control: Install Package ...
- EHCache 实现通用类 CacheManager
package com.zhubaje.api.workflow.ehcache; import java.io.Serializable; import java.util.ArrayList; i ...