给定一个模式,和一个字符串str,返回str是否符合相同的模式。
这里的符合意味着完全的匹配,所以这是一个一对多的映射,在pattern中是一个字母,在str中是一个为空的单词。
例如:
pattern = "abba", str = "dog cat cat dog" 应该返回真。
pattern = "abba", str = "dog cat cat fish" 应该返回假。
pattern = "aaaa", str = "dog cat cat dog" 应该返回假。
pattern = "abba", str = "dog dog dog dog" 应该返回假。
批注:
你可以假定pattern中只包含小写字母,在str中只包含被单个空格隔开的小写字母。
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 in pattern and a non-empty word in str.
Examples:
pattern = "abba", str = "dog cat cat dog" should return true.
pattern = "abba", str = "dog cat cat fish" should return false.
pattern = "aaaa", str = "dog cat cat dog" should return false.
pattern = "abba", str = "dog dog dog dog" should return false.
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
我发现我真是越来越爱LeetCode了 ……
今天刚做了一道类似的题目:
LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)
只不过本题是升级版的,之前是字母匹配字母,现在是字母匹配单词了。之前的题目示例如下:
For example,
Given "egg", "add", return true.
Given "foo", "bar", return false.
Given "paper", "title", return true.
当时我们实现了这么一个函数:
vector<int> getVecOrder(string str) {
map<char, int> strM;
int index = 0;
vector<int> strVec;
for (int i = 0; i < str.size(); ++i) {
auto iter = strM.find(str[i]);
if (iter == strM.end()) {
strM.insert(pair<char, int>(str[i], index));
strVec.push_back(index);
index += 1;
}
else {
strVec.push_back(strM[str[i]]);
}
}
return strVec;
}
它能够根据字符串生成序列:
For example,
Given "paper", return "01023".
Given "foo", return "011".
Given "isomorphic", return "0123245607".
现在的需求也是类似的,只不过更加升级了一点而已:
For example,
Given "dog cat cat dog", return "0110".
Given "dog cat cat fish", return "0112".
Given "Word Pattern", return "01".
所以就封装了如下函数:
vector<int> getVecOrderPro(string str) {
istringstream sstream(str);
string tempStr;
vector<string> strVec;
while (!sstream.eof()) {
getline(sstream, tempStr, ' ');
strVec.push_back(tempStr);
}
map<string, int> strNumMap;
int strNumIndex = 0;
vector<int> orderNumVec;
for (int i = 0; i < strVec.size(); ++i) {
auto iter = strNumMap.find(strVec[i]);
if (iter == strNumMap.end()) {
strNumMap.insert(pair<string, int>(strVec[i], strNumIndex));
orderNumVec.push_back(strNumIndex);
strNumIndex += 1;
}
else {
orderNumVec.push_back(strNumMap[strVec[i]]);
}
}
return orderNumVec;
}
首先需要对整个长长的字符串进行根据空格进行切割,切割成的单个的字符串并添加到vector数组中,使用了流的相关函数。
后面的部分就和之前的一样了,因为是个封装好的函数了,对变量名也进行了一定的修改,前面的那个函数由此修改如下:
vector<int> getVecOrder(string str) {
map<char, int> charNumMap;
int charNumIndex = 0;
vector<int> orderNumVec;
for (int i = 0; i < str.size(); ++i) {
auto iter = charNumMap.find(str[i]);
if (iter == charNumMap.end()) {
charNumMap.insert(pair<char, int>(str[i], charNumIndex));
orderNumVec.push_back(charNumIndex);
charNumIndex += 1;
}
else {
orderNumVec.push_back(charNumMap[str[i]]);
}
}
return orderNumVec;
}
最后的比较如下,因为题目没有说pattern和str的长度一致,也就是说如果最后的索引长度不匹配了那肯定就是false了。所以多加一行:
bool wordPattern(string pattern, string str) {
vector<int> pattern_v = getVecOrder(pattern), str_v = getVecOrderPro(str);
if (pattern_v.size() != str_v.size()) return false;
for (int i = 0; i < pattern_v.size(); ++i) {
if (pattern_v[i] != str_v[i]) return false;
}
return true;
}
class Solution {
public:
vector<int> getVecOrder(string str) {
map<char, int> charNumMap;
int charNumIndex = 0;
vector<int> orderNumVec;
for (int i = 0; i < str.size(); ++i) {
auto iter = charNumMap.find(str[i]);
if (iter == charNumMap.end()) {
charNumMap.insert(pair<char, int>(str[i], charNumIndex));
orderNumVec.push_back(charNumIndex);
charNumIndex += 1;
}
else {
orderNumVec.push_back(charNumMap[str[i]]);
}
}
return orderNumVec;
}
vector<int> getVecOrderPro(string str) {
istringstream sstream(str);
string tempStr;
vector<string> strVec;
while (!sstream.eof()) {
getline(sstream, tempStr, ' ');
strVec.push_back(tempStr);
}
map<string, int> strNumMap;
int strNumIndex = 0;
vector<int> orderNumVec;
for (int i = 0; i < strVec.size(); ++i) {
auto iter = strNumMap.find(strVec[i]);
if (iter == strNumMap.end()) {
strNumMap.insert(pair<string, int>(strVec[i], strNumIndex));
orderNumVec.push_back(strNumIndex);
strNumIndex += 1;
}
else {
orderNumVec.push_back(strNumMap[strVec[i]]);
}
}
return orderNumVec;
}
bool wordPattern(string pattern, string str) {
vector<int> pattern_v = getVecOrder(pattern), str_v = getVecOrderPro(str);
if (pattern_v.size() != str_v.size()) return false;
for (int i = 0; i < pattern_v.size(); ++i) {
if (pattern_v[i] != str_v[i]) return false;
}
return true;
}
};