LeetCode刷题实战291:单词规律II
共 4421字,需浏览 9分钟
·
2021-06-12 21:45
Given a pattern and a string str, find if strfollows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty substring in str.
给你一种规律 pattern 和一个字符串 str,请你判断 str 是否遵循其相同的规律。
这里我们指的是 完全遵循,例如 pattern 里的每个字母和字符串 str 中每个 非空 单词之间,存在着双向连接的对应规律。
示例
示例1:
输入: pattern = "abab", str = "redblueredblue"
输出: true
示例2:
输入: pattern = "aaaa", str = "asdasdasdasd"
输出: true
示例3:
输入: pattern = "aabb", str = "xyzabcxzyabc"
输出: false
提示:
你可以默认 pattern 和 str 都只会包含小写字母。
解题
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 == 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)) 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;
}
};