LintCode 58: Compare Strings

题目描述

比较两个字符串AB,确定A中是否包含B中所有的字符。字符串AB中的字符都是大写字母

样例

给出A = "ABCD" B = "ACD",返回true

给出A = "ABCD" B = "AABC", 返回false

Fri Mar 17 2017

思路

这道题跟前面一道『两个字符串是变位词』很相似,不同的只是在这题中两个字符串的长度可以不相等。

可以用同样的思路,建一个线性哈希表,统计字符串A中各字符出现的次数,然后再一次减去字符串B中各字符出现的次数,若减到小于0,则可以断定字符串B中存在字符串A中不包含的字符,返回false.

代码

// 比较字符串
class Solution {
public:
/**
* @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
*/
bool compareStrings(string A, string B) {
if (A.size() < B.size()) return false;
int count[256] = {0};
for (string::iterator iter = A.begin(); iter != A.end(); ++iter)
++count[*iter];
for (string::iterator iter = B.begin(); iter != B.end(); ++iter)
{
--count[*iter];
if (count[*iter] < 0) return false;
}
return true;
}
};

LintCode 58: Compare Strings的更多相关文章

  1. lintcode:Compare Strings 比较字符串

    题目: 比较字符串 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母 样例 给出 A = "ABCD" B = "ACD" ...

  2. 【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 ...

  3. Compare Strings

    Compare two strings A and B, determine whether A contains all of the characters in B. The characters ...

  4. LeetCode.1170-比较字符串中最小字符的出现频率(Compare Strings by Frequency of the Smallest Char)

    这是小川的第412次更新,第444篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第263题(顺位题号是1170).在一个非空字符串s上定义一个函数f(s),该函数计算s中最小字 ...

  5. 【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 ...

  6. [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 ...

  7. 【LeetCode】1170. Compare Strings by Frequency of the Smallest Character 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 日期 题目地址:https://leetc ...

  8. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  9. Core Java Volume I — 3.6. Strings

    3.6. StringsConceptually, Java strings are sequences of Unicode characters(Java的字符串是一个Unicode序列). Fo ...

随机推荐

  1. office2013 激活方法

    1.秘钥码激活 可以淘宝上买一个. 2.KMS激活软件激活 帖子地址 http://tieba.baidu.com/p/3855281630 Office 2013 Professional Plus ...

  2. [cnbeta] 波音系列飞机价格。。。

    https://www.cnbeta.com/articles/tech/786745.htm 单价最便宜的是波音737-700,为0.858亿美元(约合5.96亿元). 评论网友调侃,“你家能满40 ...

  3. [总结] Visual Studio 报价已经对比

    来源微软官方网站 对比 https://visualstudio.microsoft.com/zh-hans/vs/compare/?rr=https%3A%2F%2Fwww.ithome.com%2 ...

  4. Python基础【3】:Python中的深浅拷贝解析

    深浅拷贝 在研究Python的深浅拷贝区别前需要先弄清楚以下的一些基础概念: 变量--引用--对象(可变对象,不可变对象) 切片(序列化对象)--拷贝(深拷贝,浅拷贝) 我是铺垫~ 一.[变量--引用 ...

  5. macOS how to install python3

    macOS how to install python3 macOS & Python 3.7.2 https://www.python.org/downloads/mac-osx/ http ...

  6. javascript定时保存表单数据的代码

    (忘记是不是两家邮箱都有这个功能). 那这个功能是怎么做的呢? 定时,我们知道怎么弄,但保存呢?也许我们会通过隐藏域等手段来存放数据.但是,这个却有个缺点:那就是刷新页面后,数据将会丢失. 而此时,就 ...

  7. 在Asp.Net Core中使用Session

    1.在Stratup.cs中配置Session public void ConfigureServices(IServiceCollection services) { services.AddSes ...

  8. 【版本管理】git本地操作

    1.初始化一个Git仓库,使用git init命令. 2.添加文件到Git仓库,分两步: • 第一步,使用命令git add 文件名,注意,可反复多次使用,添加多个文件: • 第二步,使用命令git ...

  9. Sametime SDK

    1,Sametime Server A.Sametime includes many server applications, which collectively provide the capab ...

  10. APT攻击基础科普

    0x00 APT的历史起源背景 APT这个词汇最早起源于:2005年英国和美国的CERT组织发布了关于有针对性的社交工程电子邮件,放弃特洛伊木马以泄露敏感信息的第一个警告,尽管没有使用“APT”这个名 ...