代码拉取完成,页面将自动刷新
// Time: O(n * C(n - 1, c - 1)), n is length of str, c is unique count of pattern,
// there are 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 {
public:
bool wordPatternMatch(string pattern, string str) {
unordered_map<string, char> w2p;
unordered_map<char, string> p2w;
return match(pattern, str, 0, 0, &w2p, &p2w);
}
bool match(const string &pattern, const string &str,
const int i, const int j,
unordered_map<string, char>* w2p,
unordered_map<char, string>* p2w) {
bool is_match = false;
if (i == pattern.length() && j == str.length()) {
is_match = true;
} else if (i < pattern.length() && j < str.length()) {
const char p = pattern[i];
if (p2w->count(p)) {
const auto& w = (*p2w)[p];
if (w == str.substr(j, w.length())) { // Match pattern.
is_match = match(pattern, str, i + 1, j + w.length(), w2p, p2w);
} // Else return false.
} else {
for (int k = j; k < str.length() && !is_match; ++k) {
const string w = str.substr(j, k - j + 1);
if (!w2p->count(w)) {
// Build mapping. Space: O(n + c)
(*w2p)[w] = p, (*p2w)[p] = w;
is_match = match(pattern, str, i + 1, k + 1, w2p, p2w);
w2p->erase(w), p2w->erase(p);
} // Else try longer word.
}
}
}
return is_match;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。