Leetcode--1

Source: Internet
Author: User
Tags str words

From the national day to now have not updated this month is also really miserable the first half of the month has been in the doctor melancholy most of the months fortunately, it is easy to thank that period of time friends of the company to love themselves is ah after watching more than a week of Hadoop helpless always appear all kinds of problems actually really want to feel the magic of Hadoop But for the time being put aside for a few days to see maybe the problem solved it is not necessarily today just finished join a new team feel very good ah always have a more than their own dream of the feeling but the heart is still pretty sad perhaps this period of the struggle to let oneself a bit disappointed There are a lot of dreams that you want to achieve. Some dreams are for 1 million reasons I don't want to give up

Well, let's start from today, look at the algorithms in Leetcode. First or the simple start  

Wordpattern

(1) Restatement of the problem

Given a pattern and a str, determine whether the input str satisfies the same pattern.

For example:

Pattern = "ABBA", str = "Dog cat Cat Dog" should return true.

Pattern = "ABBA", str = "Dog cat cat Fish" should return false.

Pattern = "AAAA", str = "Dog cat Cat Dog" should return false.

Pattern = "ABBA", str = "dog dog dog" should return false.

(2) Problem analysis

Judging whether Str satisfies the same pattern, according to the given instance, the problem must satisfy two conditions: the same length of pattern and STR, and the same word in the pattern where the same words are located in Str.

Two conditions must be satisfied at the same time, the same length can be directly with Len (), for the 2nd, the first thought is dict, and in order to simplify the code, combined with set () and zip () further optimization, the first obtained code is as follows:

 class   Solution (object):  def   Wordpattern (self, Pattern, str):  " ""  : type Pattern:str:type str:str:rtype:bool   Self.pattern  = pattern self.str  = str words  = s Tr.split ()  if  len (pattern)! = Len (str) or  Set (Zip (pattern,words))!= set (pattern):  return   False  return  True 

Further optimizations are:

class solution (Object):     def Wordpattern (self, Pattern, str):               """         : Type pattern:str        : Type str:str        : Rtype:bool        """        =  Pattern        = str        = str.split ()        returnor set (Zip ( pattern,words))! = Set (pattern)

In combination with runtime, the code does have a certain degree of optimization. Well, I can only do it that way.

Ben thought to look at other things, but the situation is not very ideal, ah, early rest, good night.

I hope everything will be all right tomorrow.

Leetcode--1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.