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. 简单说明webbench的安装和使用

    简介 运行在linux上的一个性能测试工具 官网地址:http://home.tiscali.cz/~cz210552/webbench.html 如果不能打开的话,也可以直接到网盘下载:http:/ ...

  2. 【vue】router-link 与 router-view

    1 router-link <router-link :to="{ path: '/hello', component: HelloWorld }">hello< ...

  3. SpringBoot(四)_Spring Data JPA的使用

    JPA 绝对是简化数据库操作的一大利器. 概念 首先了解 JPA 是什么? JPA(Java Persistence API)是 Sun 官方提出的 Java 持久化规范.它为 Java 开发人员提供 ...

  4. JavaScript的setTimeout()和setInterval()

    1. setTimeout()方法 作用:在制定的毫秒数后调用函数或计算表达式 语法: setTimeout(code,millisec) 实例: function timedMsg() { var ...

  5. IE 低版本 透明度

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 第217天:深入理解Angular双向数据绑定的原理

    一.理解angular双向数据绑定 双向绑定是新的前端框架中频繁出现的一个新词汇,也是mvvm的核心原理.angularjs五条核心信念中的数据驱动,便是由双向绑定进行完成. 那么什么是双向绑定,下面 ...

  7. 每日一问(常用的集合接口和类有哪些【二】)—最常用的集合ArrayList类

    本人在曾经的数年编程生涯中,使用的最多的就是ArrayList类了,原因也非常简单.ArrayList类可以是最直接符合集合这一概念的类了,当然这种说法只是我的个人之见.ArrayList可以说是一个 ...

  8. selenium基础-打开百度进行搜索

    1. 安装Python 2. 安装selenium 3. 下载谷歌驱动ChromeDriver,放到Python的Scripts目录下 4. 编写代码,如下 # coding: utf-8 from ...

  9. 快速配置java环境变量

    右键单击计算机--->属性 点击 “高级系统设置”--->"环境变量",出现环境变量设置窗口 系统变量--->新建 JAVA_HOME变量,变量值填写jdk安装路 ...

  10. [CF850F] Rainbow Balls

    题目大意 这里 题解 我们枚举最后剩下的球的种类,那么其他球可以看做没用了. 设选定的球有\(a_i\)个,球的总数为\(s=\sum_{i=1}^n a_i\). 现在问题变为:在一个长度为\(s\ ...