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:
Pattern = "ABBA", str = "Dog cat Cat Dog", returns true
Pattern = "ABBA", str = "Dog cat cat Fish", return false
Pattern = "AAAA", str = "Dog cat Cat Dog", return false
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