Leetcode:word pattern-string pattern matching

Source: Internet
Author: User

1. Title

Word pattern (string pattern match)

2. Address of the topic

https://leetcode.com/problems/word-pattern/

3. Topic content

English: Given a pattern and a string str , find if str follows the same pattern.

English: Give a set of patterns (pattern) and a string (str) to see if the string matches the pattern

For example:

    1. Pattern = "ABBA", str = "Dog cat Cat Dog", returns true

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

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

    4. Pattern = "ABBA", str = "Dog dog Dog", return false

Attention:

The pattern has only lowercase letters, the strings are separated by a single space character, each word in the string is made up of lowercase letters, and neither the pattern nor the string contains extra spaces, and each letter in the pattern must match a word with a length of at least 1 in a string.

4. Method of solving Problems 1

This problem can be easily solved by using HashMap.

Note that there are two different characters in the pattern that cannot correspond to the same word in the string.

The Java code is as follows:

import java.util.hashmap;/** * @ function Description: leetcode 290 - word pattern *  @ Developer: tsybius2014 * @ Development Date: October 9, 2015  */public class Solution {         /**     *  string pattern matching       *  @param  pattern     *  @param  str      *  @return      */    public boolean  Wordpattern (STRING&NBSP;PATTERN,&NBSP;STRING&NBSP;STR)  {                 if  (Pattern.isempty ()  | |  str.isempty ())  {            return  false;        }                 string[] s = str.split (" ");        if  (s.length !=  pattern.length ())  {            return  false;        }                 hashmap<character, string> hashmap =  new HashMap<Character, String> (        for);   (Int i = 0; i < pattern.length ();  i++)  {             if  (Hashmap.containskey (Pattern.charat (i)))  {                 if  (! Hashmap.get (Pattern.charat (i)). Equals (S[i]))  {                     return false;                 }            }  else if  (Hashmap.containsvalue (s[i))  {                 return false;             } else {                 hashmap.put (Pattern.charat (i),  s[i]);             }        }                 return true;     }}

5. Method of solving Problems 2

Another approach is to take advantage of the properties of the put function in HashMap.

The PUT function is declared as follows:

Public V put (K key, V value)

Its function is to store the key-value pairs in the map, and replace the old values with the new values if the map contains the key to be inserted. For the return value of the function, if the key to be inserted does not exist in the dictionary, NULL is returned, otherwise the value before the substitution is returned. Depending on the nature of the put function, the following Java code can be made:

import java.util.hashmap;import java.util.objects;/** * @ function Description: LeetCode 290 -  word pattern * @ Developer: tsybius2014 * @ Development Date: October 9, 2015  */public class  Solution {        /**     *  Pattern matching      *  @param  pattern     *  @param   str     *  @return      */    public  boolean wordpattern (STRING&NBSP;PATTERN,&NBSP;STRING&NBSP;STR)  {                 if  (Pattern.isempty ()  | |  str.isempty ())  {            return  false;        }                 string[] s = str.split (" ");         if   (S.length != pattern.length ())  {             return false;        }                  @SuppressWarnings ("Rawtypes")          hashmap<comparable, integer> hashmap = new  HashMap<Comparable, Integer> ();        for  (int  i = 0; i < pattern.length ();  i++)  {             if  (! Objects.equals (Hashmap.put (Pattern.charat (i),  i),  hashmap.put (s[i], i))                  return false;        }                 return true;    }}

END

Leetcode:word pattern-string pattern matching

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.