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. Android之自定义View学习(一)

    Android之自定义View学习(一) Canvas常用方法: 图片来源 /** * Created by SiberiaDante on 2017/6/3. */ public class Bas ...

  2. mysubmail 短信报警

    https://www.mysubmail.com/chs/documents/developer/YPWD84   文本文档  官网:www.mysubmail.com 操作流程:快速接入短信 AP ...

  3. go 面试题总结

    1.什么是goroutine,他与process, thread有什么区别? 2. 什么是channel,为什么它可以做到线程安全? 3. 了解读写锁吗,原理是什么样的,为什么可以做到? 4. 如何用 ...

  4. [转帖] 中国SaaS死或生之二: ERP两大邪术,尽出歪门邪路 ---- 挺好玩的

    中国SaaS死或生之二: ERP两大邪术,尽出歪门邪路   http://www.cniteyes.com/archives/33753   文章摘要:在数字化浪潮中,油腻ERP大叔的那些“歪门邪术” ...

  5. swusec的构想,顺便送开学福利——校园网一号多登录演示

    前言: 我不是什么大牛,我只想通过我的努力,打造swu网络安全爱好者的圈子.期待你加入. swusec是什么? swusec (SouthWestUniversity SecurityTeam),西南 ...

  6. 关于MyEclipse,JDK使用的几点收获

    [1]MyEclipse如何修改JDK编译版本信息 首先打开MyEclipse——>windows——>preference(也就是 窗口——>首选项:可以在搜索框中输入JDK,查找 ...

  7. [BinaryTree] AVL树、红黑树、B/B+树和Trie树的比较

    转自:AVL树.红黑树.B/B+树和Trie树的比较 AVL树 最早的平衡二叉树之一.AVL是一种高度平衡的二叉树,所以通常的结果是,维护这种高度平衡所付出的代价比从中获得的效率收益还大,故而实际的应 ...

  8. SQL利用Case When Then多条件

    CASE    WHEN 条件1 THEN 结果1    WHEN 条件2 THEN 结果2    WHEN 条件3 THEN 结果3    WHEN 条件4 THEN 结果4.........    ...

  9. path变量修改后无法保存

    Eclipse启动时出现错误: A Java Runtime Environment (JRE) or Java Development Kit(JDK) must be available in o ...

  10. jmeter函数

    1.常用JMeter函数 1)__regexFunction 正则表达式函数可以使用正则表达式(用户提供的)来解析前面的服务器响应(或者是某个变量值).函数会返回一个有模板的字符串,其中携带有可变的值 ...