zoukankan      html  css  js  c++  java
  • 【leetcode❤python】 290. Word Pattern

    #-*- 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')          

  • 相关阅读:
    codechef FNCS
    bzoj2653 middle
    CF698F Coprime Permutation
    CF538H Summer Dichotomy
    CF930E Coins Exhibition
    CF468D Tree
    CF528E Triangles3000
    BZOJ 4066: 简单题
    BZOJ 4300: 绝世好题
    BZOJ 4520: [Cqoi2016]K远点对
  • 原文地址:https://www.cnblogs.com/kwangeline/p/6059542.html
Copyright © 2011-2022 走看看