Compare Strings
Compare two strings A and B, determine whether A contains all of the characters in B.
The characters in string A and B are all Upper Case letters.
Notice
The characters of B in A are not necessary continuous or ordered.
For A = "ABCD"
, B = "ACD"
, return true
.
For A = "ABCD"
, B = "AABC"
, return false
.
分析:
用array记录每个字符在A中出现的个数,然后再减去B中的字符,如果不够,说明不包含。
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) {
if (A == null & B != null) return false;
if (A == null && B == null) return true;
if (B == null) return true; int[] charCounts = new int[]; for (int i = ; i < A.length(); i++) {
charCounts[A.charAt(i)]++;
} for (int i = ; i < B.length(); i++) {
if (charCounts[B.charAt(i)] == ) {
return false;
}
charCounts[B.charAt(i)]--;
}
return true;
}
}
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 ...
- lintcode:Compare Strings 比较字符串
题目: 比较字符串 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母 样例 给出 A = "ABCD" B = "ACD" ...
- LeetCode.1170-比较字符串中最小字符的出现频率(Compare Strings by Frequency of the Smallest Char)
这是小川的第412次更新,第444篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第263题(顺位题号是1170).在一个非空字符串s上定义一个函数f(s),该函数计算s中最小字 ...
- 【leetcode】1170. Compare Strings by Frequency of the Smallest Character
题目如下: Let's define a function f(s) over a non-empty string s, which calculates the frequency of the ...
- [LC] 1170. Compare Strings by Frequency of the Smallest Character
Let's define a function f(s) over a non-empty string s, which calculates the frequency of the smalle ...
- 【LeetCode】1170. Compare Strings by Frequency of the Smallest Character 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 日期 题目地址:https://leetc ...
- Core Java Volume I — 3.6. Strings
3.6. StringsConceptually, Java strings are sequences of Unicode characters(Java的字符串是一个Unicode序列). Fo ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
随机推荐
- (Struts)ActionForm类及表单数据验证
LoginForm代码: /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ pac ...
- 更新java对xml文件的操作
//更新java在xml文件中操作的内容 public static void upda(Document doc) throws Exception{ //创建一个TransformerFactor ...
- C语言中memset(void *s, char ch,unsigned n)用的用法
将指针s所指的内存空间中前n为重置为字符c 程序例: #include <string.h> #include <stdio.h> #include <memory.h& ...
- Hibernate-一对多的关系维护
一对多 和多对一 一般是看需求来确定的,很多时候都是设置成双向的 举个最最普通的离子 :一个班级里面有多个学生 多个学生属于一个班级 从学生表来看 就是多对一的关系 从班级表来看就是一对多的关系 需求 ...
- javascript显示实时时间
<html> <script language=Javascript> function time(){ //获得显示时间的div t_div = document.getEl ...
- MySQL学习笔记01-MYSQL安装
一 MySQL简介 MySQL是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司. MySQL 最流行的关系型数据库管理系统. MySQL分为企业版和社区版. ...
- knockoutjs + easyui.treegrid 可编辑的自定义绑定插件
http://blog.csdn.net/maddemon/article/details/16846183 目前仅支持URL的CRUD.不需要的话可以却掉相关代码,把treegrid的data直接赋 ...
- soa vs cop
soa强调分层:底层为高层提供服务: cop强调分块:有明确的职责和服务提供接口,为外部提供服务. SOA 原则非常强调将服务使用者和服务提供者分离开来,关于此类分离实际的含义,有很多不正式但非常有用 ...
- 慎用 Enum.GetHashCode()
公司里遗留下了相当多的 Enum.GetHashCode()来获取枚举值的代码 但是这会产生装箱行为的!!因为Enum是值类型,GetHashCode()是Object的方法,调用GetHashCod ...
- spark.SecurityManager: SecurityManager: authentication disabled