zoukankan      html  css  js  c++  java
  • lintcode-828. 字模式

    • 题目描述:

    828.字模式

    给定一个模式和一个字符串str,查找str是否遵循相同的模式。
    这里遵循的意思是一个完整的匹配,在一个字母的模式和一个非空的单词str之间有一个双向连接的模式对应。

    样例

    给定模式= "abba", str = "dog cat cat dog",返回true。给定模式= "abba", str = "dog cat cat fish",返回false
    给定模式= "aaaa", str = "dog cat cat dog",返回false。给定模式= "abba", str = "dog dog dog dog",返回false

    • 分析

    字符匹配问题,将映射写成字符对的形式,将pattern中字符’a’映射到str中’dog’,如('a','dog'),如果映射个数与pattern中字符种类相同,则匹配成功。

    set函数:

    创建一个无序不重复元素集

    zip函数:

    zip([iterable, ...])
    用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。

    • code 

    class Solution(object):
        def wordPattern(self, pattern, str):
            """
            :type pattern: str
            :type str: str
            :rtype: bool
            """
            words = str.split(' ')
            if len(words) != len(pattern):
                return False
            return len(set(pattern)) == len(set(words)) == len(set(zip(pattern, words)))
    • 参考链接

    https://blog.csdn.net/coder_orz/article/details/51693647

  • 相关阅读:
    正则表达式
    查看当前文件大小
    logging日志快速上手
    kafka消息队列的使用
    修改文件权限给指定的用户
    使用Dockerfile构建镜像
    k8s 常用命令总结
    k8s pod.yaml配置文件参数
    Linux安装依赖包
    Freeswitch配置SIP网关拨打外部
  • 原文地址:https://www.cnblogs.com/yeshengCqupt/p/9869659.html
Copyright © 2011-2022 走看看