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 ...
随机推荐
- 模拟抛硬币(C语言实现)
实现代码: #include<stdio.h> #include<stdlib.h> int heads() { ; } int main(int argc, char *ar ...
- php多层数组与对象的转换实例代码
通过json_decode(json_encode($object)可以将对象一次性转换为数组,但是object中遇到非utf-8编码的非ascii字符则会出现问题,比如gbk的中文,何况json_e ...
- Golang学习笔记
一.基础 1. Hello World程序 demo: package main import "fmt" // 注释 //注释 func main() { fmt.Printf( ...
- WPF-数据绑定:日期时间格式
WPF-数据绑定:日期时间格式绑定后自定义格式的例子. 我刚才遇到的问题是绑定完之后,星期始终显示为英文.需要一个属性ConverterCulture制定区域. 如下: {Binding dateti ...
- 【转】为什么我说 Android 很糟糕
http://zhuanlan.zhihu.com/wooyun/19879016 Android 的安全问题一直被吐槽,包括不安全的APP市场.上次的远程命令执行漏洞.还有它的权限机制,总之一团糟, ...
- posix 消息队列
注意 在涉及到posix消息的函数时, gcc 编译时要加-lrt参数, 如 gcc -lrt unpipc.c mqpack.c send.c -o send gcc -lrt unpipc.c m ...
- 【html5】这些新类型 能提高生产力
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
- 从零开始学ios开发(十二):Table Views(上)
这次学习的控件非常重要且非常强大,是ios应用中使用率非常高的一个控件,可以说几乎每个app都会使用到它,它就是功能异常强大的Table Views.可以打开你的iphone中的phone.Messa ...
- c#之反射总结
1.了解什么事程序集 2.加载程序集 首先要加载需要加载的程序集,然后找到指定的类型,进而往下进行动态加载. 要加载的程序集中的内容: public class Class1:Person { pr ...
- SQL Server2008附加数据库之后显示为只读
SQL Server2008附加数据库之后显示为只读时解决方法 啰嗦的话就不多说了,直入主题吧! 方案一: 碰到这中情况一般是使用的sa账户登录的,只要改为Windows身份验证,再附加数据库即可搞定 ...