LeetCode 题解(271) : Word Pattern II
来源:互联网 发布:苹果手机照片传不到mac 编辑:程序博客网 时间:2024/06/05 05:23
题目:
Given a pattern
and a string str
, find if str
follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter inpattern
and a non-empty substring in str
.
Examples:
- pattern =
"abab"
, str ="redblueredblue"
should return true. - pattern =
"aaaa"
, str ="asdasdasdasd"
should return true. - pattern =
"aabb"
, str ="xyzabcxzyabc"
should return false.
Notes:
You may assume both pattern
and str
contains only lowercase letters.
比较典型的Backtracing,注意更新hashmap的时候不要犯错。
C++版:
class Solution {public: bool wordPatternMatch(string pattern, string str) { unordered_map<char, string> d; unordered_map<string, bool> used; return match(pattern, str, d, used); } bool match(string pattern, string str, unordered_map<char, string> &d, unordered_map<string, bool> &used) { if(pattern.length() == 0 && str.length() == 0) return true; if(pattern.length() == 0 || str.length() == 0) return false; if(pattern.length() == 1) { if(d.find(pattern[0]) == d.end()) { if(used.find(str) == used.end()) return true; else return false; } else { if(d[pattern[0]] == str) return true; else return false; } } if(d.find(pattern[0]) != d.end()) { if(str.find(d[pattern[0]]) != 0) return false; else { bool result = match(pattern.substr(1), str.substr(d[pattern[0]].length()), d, used); if(result) return true; } } else { int length = str.length() - (pattern.length() - 1); for(int i = 1; i <= length; i++) { string cur = str.substr(0, i); d.insert(pair<char, string>(pattern[0], cur)); used.insert(pair<string, bool>(cur, true)); bool result = match(pattern.substr(1), str.substr(cur.length()), d, used); d.erase(pattern[0]); used.erase(cur); if(result) return true; } } return false; }};
Python版:
class Solution(object): def wordPatternMatch(self, pattern, str): """ :type pattern: str :type str: str :rtype: bool """ d, used = {}, {} return self.match(pattern, str, d, used) def match(self, pattern, str, d, used): if len(pattern) == 0 and len(str) == 0: return True elif len(pattern) == 0 or len(str) == 0: return False if len(pattern) == 1: if pattern in d: if d[pattern] == str: return True else: return False else: if str in used and used[str]: return False else: return True if pattern[0] in d: if str.find(d[pattern[0]]) != 0: return False else: result = self.match(pattern[1:], str[len(d[pattern[0]]):], d, used) if result: return True else: length = len(str) - (len(pattern) - 1) for i in range(length): cur = str[:i+1] if cur in used and used[cur]: continue used[cur] = True d[pattern[0]] = cur result = self.match(pattern[1:], str[len(cur):], d, used) del d[pattern[0]] if result: return True del used[cur] return False
0 0
- LeetCode 题解(271) : Word Pattern II
- LeetCode题解:Word Pattern
- leetcode Word Pattern II
- LeetCode 题解(257) : Word Pattern
- LeetCode 291. Word Pattern II
- LeetCode - Word Ladder II 题解
- LeetCode - Word Break / Word Break II 题解
- Leetcode 290. Word Pattern & 291. Word Pattern II
- [leetcode] 291. Word Pattern II 解题报告
- [Leetcode] 291. Word Pattern II 解题报告
- LeetCode题解:Word Break I and II
- LeetCode 题解(87): Word Break II
- LeetCode 题解(92): Word Search II
- LeetCode 题解(95): Word Ladder II
- LeetCode 题解(268) : Shortest Word Distance II
- leetcode题解-126. Word Ladder II
- LeetCode 291. Word Pattern II(单词模式II)
- leetcode题解-205.Isomorphic Strings && 290. Word Pattern
- java传入相关的时间可以获取这个时间段的星期时间
- Ember.js 入门指南——包裹内容
- 数据库优化原则2
- [C/C++基础知识] 一篇就让你彻底搞懂qsort快速排序的文章
- PHP第一季视频教程.李炎恢.学习笔记(三)(第2章 基本语法(2))
- LeetCode 题解(271) : Word Pattern II
- Rails读书笔记第一章
- txt文档内容入mysql
- grunt快速上手(转)
- Rails读书笔记第二章
- excel 宏(VB)实现复制一列并按第二列的单元格长度排序
- excel减肥过滤广告
- 合并子目录所有文本文件的批处理
- [机器学习] Adaboost算法流程及原理推导