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. aes python加密

    # *_*coding:utf-8 *_* #AES-demo import base64 from Crypto.Cipher import AES ''' 采用AES对称加密算法 ''' # st ...

  2. .NET Core 中的并发编程

    今天我们购买的每台电脑都有一个多核心的 CPU,允许它并行执行多个指令.操作系统通过将进程调度到不同的内核来发挥这个结构的优点. 然而,还可以通过异步 I/O 操作和并行处理来帮助我们提高单个应用程序 ...

  3. Dubbo学习(五) Dubbo 从下载到编译成功

    DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广 ...

  4. C++ STL 常用拷贝和替换算法

    C++ STL 常用拷贝和替换算法 copy() 复制 vector<int> vecIntA; vecIntA.push_back(1); vecIntA.push_back(3); v ...

  5. R1 学习记录

    libevent框架学习特点: 1.可移植行,跨平台的 2.速度快,libevent会用各平台最快的非阻塞IO函数 3.扩展性 4.方便性构成: 1.evutil: 抽象出各平台network的函数 ...

  6. hdu6057 Kanade's convolution 【FWT】

    题目链接 hdu6057 题意 给出序列\(A[0...2^{m} - 1]\)和\(B[0...2^{m} - 1]\),求所有 \[C[k] = \sum\limits_{i \; and \; ...

  7. 洛谷 P1878 舞蹈课 解题报告

    P1878 舞蹈课 题目描述 有\(n\)个人参加一个舞蹈课.每个人的舞蹈技术由整数来决定.在舞蹈课的开始,他们从左到右站成一排.当这一排中至少有一对相邻的异性时,舞蹈技术相差最小的那一对会出列并开始 ...

  8. 单点登录(十二)-----遇到问题-----cas启用mongodb验证方式登录后没反应-pac4j-mongo包中的MongoAuthenticatInvocationTargetException

    cas启用mongodb验证方式登录后没反应 控制台输出 2017-02-09 20:27:15,766 INFO [org.jasig.cas.authentication.MongoAuthent ...

  9. Android打包 & Gradle用法

    Ref: Maven仓库上查插件最新版本号The Central Repository Search Enginehttp://search.maven.org/#search%7Cga%7C1%7C ...

  10. as, idea 出现 Gradle's dependency cache may be corrupt 错误分析

    问题: Error:Failed to open zip file.Gradle's dependency cache may be corrupt (this sometimes occurs af ...