#-*- coding: UTF-8 -*-
class Solution(object):
    def wordPattern(self, pattern, str):
        """
        :type pattern: str
        :type str: str
        :rtype: bool
        """
        tag=0
        tagdic={}
        tagList=[]
        i=0
        while i<len(pattern):
            if tagdic.has_key(pattern[i]):
                tagList.append(tagdic.get(pattern[i]))
            else:
                tagdic.setdefault(pattern[i],tag)
                tagList.append(tag)
                tag+=1
            i+=1
        strList=str.split(' ')
   
        tagdic={};tag=0;tagList2=[];i=0
   
        while i<len(strList):
         
            if tagdic.has_key(strList[i]):
                tagList2.append(tagdic.get(strList[i]))
            else:
                tagdic.setdefault(strList[i],tag)
                tagList2.append(tag)
                tag+=1
            i+=1
        
        return tagList==tagList2

sol=Solution()
print sol.wordPattern('abba', 'dog cat cat dog')

【leetcode❤python】 290. Word Pattern的更多相关文章

  1. 【leetcode】290. Word Pattern

    problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...

  2. 【leetcode❤python】Sum Of Two Number

    #-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...

  3. 【LeetCode】290. Word Pattern 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【一天一道LeetCode】#290. Word Pattern

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 【leetcode❤python】 58. Length of Last Word

    #-*- coding: UTF-8 -*-#利用strip函数去掉字符串去除空格(其实是去除两边[左边和右边]空格)#利用split分离字符串成列表class Solution(object):   ...

  6. 【leetcode❤python】 1. Two Sum

    #-*- coding: UTF-8 -*- #AC源码[意外惊喜,还以为会超时]class Solution(object):    def twoSum(self, nums, target):  ...

  7. 【leetcode❤python】 8. String to Integer (atoi)

    #-*- coding: UTF-8 -*-#需要考虑多种情况#以下几种是可以返回的数值#1.以0开头的字符串,如01201215#2.以正负号开头的字符串,如'+121215':'-1215489' ...

  8. 【leetcode❤python】 165. Compare Version Numbers

    #-*- coding: UTF-8 -*-class Solution(object):    def compareVersion(self, version1, version2):       ...

  9. 【leetcode❤python】 168. Excel Sheet Column Title

    class Solution(object):    def convertToTitle(self, n):        """        :type n: in ...

随机推荐

  1. Tomcat上的项目部署到WebLogic上の注意事项

    1.修改web.xml: <!-- <display-name>weboutweb</display-name> --> <!-- 注释掉 display-n ...

  2. Yii多表关联

    表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer    (id  customer_name) 订单表Order           (id  order_name       ...

  3. NOIP201208同余方程

    NOIP201208同余方程 描述 求关于x的同余方程ax ≡ 1 (mod b)的最小正整数解. 格式 输入格式 输入只有一行,包含两个正整数a, b,用一个空格隔开. 输出格式 输出只有一行,包含 ...

  4. 赤手空拳编写C#代码

    有时候服务器上并没有安装任何IDE或典型的代码编辑器,只能完全手写C#代码. 不妨假设一台全新的PC,较新版本的Windows自带了.net框架,无需开发工具即可编程了. 除了以往的Bat批处理.VB ...

  5. 在 mysql 中利用 Duplicate key, 一句话实现存在的更新不存在插入功能

    mysql 中可以用一个sql命令实现在插入时,如果发现唯一索引重复的记录则自动改为更新语句, 语句如下: '; 注意,radcheck 表中 username 和 attribute 列是个组合的唯 ...

  6. Java 使用 Redis | 菜鸟教程

    入门教程: http://www.runoob.com/redis/redis-java.html 中文手册: http://redis.readthedocs.io/en/2.4/index.htm ...

  7. move语义和右值引用

    C++11支持move语义,用以避免非必要拷贝和临时对象. 具体内容见收藏中的“C++右值引用” .

  8. [ios][swift]swift GPS传感器的调用

    在Info.plist文件中添加如下配置:(1)NSLocationAlwaysUsageDescription(2)NSLocationWhenInUseUsageDescription swift ...

  9. ACM题目————The partial sum problem

    描述 One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choo ...

  10. 深入学习netty系列(1)

    一.Server端的编程模型 示例代码1 EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup ...