Given a pattern and a string str , find if str follows the same pattern.
Here follow means a full match, such that there was a bijection between a letter in and pattern a non-empty substring in .
Examples:
- pattern =
"abab" , str = "redblueredblue" should return true.
- pattern =
"aaaa" , str = "asdasdasdasd" should return true.
- pattern =
"aabb" , str = "xyzabcxzyabc" should return FALSE.
Notes:
Assume both and pattern contains only str lowercase letters.
290. Word Pattern extension, the difference is that there is no space for the word string, you can not press the space to split the word directly.
You can use backtracking to determine each case, using a hash table to establish the mapping between the pattern character and the word, but also the variables P and R to record the current recursion to the pattern character and the position of the word string, in the recursive function, if p and r are equal to the length of the pattern string and the word string, indicating that the match is successful, Returns ture, otherwise if one is reached and the other does not, the match fails and returns false.
Solution: Backtracking Backtracking
Python:
# time:o (n * C (n-1, c-1)), n is length of STR, and C is unique count of pattern,# there a Re H (n-c, c-1) = C (n-1, c-1) possible splits of string,# and each one costs O (n) to Check if it matches the word pattern.# space:o (n + C) class solution (object): Def wordpatternmatch (self, pattern, str) : "": Type Pattern:str:type str:str:rtype:bool "" "w2p, p2w = {}, {} return Self.match (pattern, str, 0, 0, w2p, p2w) def match (self, pattern, str, I, J, w2p, p2w): Is_match = Fals e if i = = Len (pattern) and J = = Len (str): Is_match = True Elif i < Len (pattern) and J < Len (str): p = pattern[i] if p in p2w:w = p2w[p] if w = = Str[j:j+len (w)]: # Match pattern. Is_match = Self.match (pattern, str, i + 1, j + Len (W), w2p, p2w) # Else return FALSE. Else:for K in Xrange (J, Len (str)): # Try any possible word w = str[j:k+1] If w not in w2p: # Build mapping. Space:o (n + C) w2p[w], p2w[p] = p, W; Is_match = Self.match (pattern, str, i + 1, K + 1, w2p, p2w) W2p.pop (W), P2w.pop (p); If Is_match:break return Is_match
C + +:
Class Solution {Public:bool Wordpatternmatch (string pattern, string str) {Unordered_map<char, string> m; Return helper (pattern, 0, str, 0, M); } bool Helper (string pattern, int p, string str, int r, Unordered_map<char, string> &m) {if (P = = Pat Tern.size () && r = = Str.size ()) return true; if (p = = pattern.size () | | | r = = str.size ()) return false; char C = pattern[p]; for (int i = r; I < Str.size (); ++i) {String t = Str.substr (R, I-r + 1); if (M.count (c) && m[c] = = t) {if (helper (pattern, p + 1, str, i + 1, m)) return true; } else if (!m.count (c)) {bool b = false; for (auto it:m) {if (It.second = = T) b = true; } if (!b) {m[c] = t; if (helper (pattern, p + 1, str, i + 1, m)) return true; M.erase (c); }}} return false; }};
C + +:
Class Solution {Public:bool Wordpatternmatch (string pattern, string str) {Unordered_map<char, string> m; Set<string> s; Return helper (pattern, 0, str, 0, M, s); } bool Helper (string pattern, int p, string str, int r, Unordered_map<char, string> &m, set<string> &am P;s) {if (P = = Pattern.size () && r = = Str.size ()) return true; if (p = = pattern.size () | | | r = = str.size ()) return false; char C = pattern[p]; for (int i = r; I < Str.size (); ++i) {String t = Str.substr (R, I-r + 1); if (M.count (c) && m[c] = = t) {if (helper (pattern, p + 1, str, i + 1, m, s)) return true; } else if (!m.count (c)) {if (S.count (t)) continue; M[C] = t; S.insert (t); if (helper (pattern, p + 1, str, i + 1, m, s)) return true; M.erase (c); S.erase (t); } } return false; }};
[Leetcode] 291. Word Pattern II Word Mode II