博客
关于我
LintCode 55 比较字符串(compare string)
阅读量:323 次
发布时间:2019-03-04

本文共 653 字,大约阅读时间需要 2 分钟。

为了确定字符串A是否包含字符串B的所有字符,我们可以采用以下方法:首先统计B中每个字符的出现次数,然后统计A中每个字符的出现次数。最后比较这两个统计结果,确保A中每个字符的数量不少于B中的数量。

步骤如下:

  • 统计字符串B中每个字符的出现次数。
  • 统计字符串A中每个字符的出现次数。
  • 检查B中的每个字符在A中的出现次数是否足够多。
  • 如果所有字符都满足条件,返回true;否则返回false。
  • 这种方法确保了即使字符在A中不是连续的,也能正确判断是否包含所有必要字符。

    示例代码:

    class Solution:    def compareStrings(self, A, B):        # 统计B中的字符频率        count_B = {}        for char in B:            count_B[char] = count_B.get(char, 0) + 1        # 统计A中的字符频率        count_A = {}        for char in A:            count_A[char] = count_A.get(char, 0) + 1        # 比较        for char in count_B:            if count_A.get(char, 0) < count_B[char]:                return False        return True

    转载地址:http://svuh.baihongyu.com/

    你可能感兴趣的文章
    poj 3083 Children of the Candy Corn
    查看>>
    POJ 3083 Children of the Candy Corn 解题报告
    查看>>
    POJ 3253 Fence Repair C++ STL multiset 可解 (同51nod 1117 聪明的木匠)
    查看>>
    Qt笔记——控件总结
    查看>>
    poj 3262 Protecting the Flowers 贪心
    查看>>
    poj 3264(简单线段树)
    查看>>
    Qt笔记——布局管理三件套分割窗口、停靠窗口和堆栈窗口
    查看>>
    poj 3277 线段树
    查看>>
    POJ 3349 Snowflake Snow Snowflakes
    查看>>
    POJ 3411 DFS
    查看>>
    poj 3422 Kaka's Matrix Travels (费用流 + 拆点)
    查看>>
    Qt笔记——官方文档全局定义(二)Functions函数
    查看>>
    POJ 3468 A Simple Problem with Integers
    查看>>
    poj 3468 A Simple Problem with Integers 降维线段树
    查看>>
    poj 3468 A Simple Problem with Integers(线段树 插线问线)
    查看>>
    poj 3485 区间选点
    查看>>
    poj 3518 Prime Gap
    查看>>
    poj 3539 Elevator——同余类bfs
    查看>>
    Qt笔记——官方文档全局定义(三)Macros宏
    查看>>
    poj 3628 Bookshelf 2
    查看>>