290. Word Pattern
Given a pattern and a string s, find if s 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 s.
Example 1:
Input: pattern = “abba”, s = “dog cat cat dog”
Output: true
Example 2:
Input: pattern = “abba”, s = “dog cat cat fish”
Output: false
Example 3:
Input: pattern = “aaaa”, s = “dog cat cat dog”
Output: false
Constraints:
1 <= pattern.length <= 300
pattern contains only lower-case English letters.
1 <= s.length <= 3000
s contains only lowercase English letters and spaces ’ '.
s does not contain any leading or trailing spaces.
All the words in s are separated by a single space.
solution1 哈希表
class Solution {
public:
bool wordPattern(string pattern, string str) {
unordered_map<string, char> str2ch;
unordered_map<char, string> ch2str;
int m = str.length();
int i = 0;
for (auto ch : pattern) {
if (i >= m) {
return false;
}
int j = i;
while (j < m && str[j] != ' ') j++;
const string &tmp = str.substr(i, j - i);
if (str2ch.count(tmp) && str2ch[tmp] != ch) {
return false;
}
if (ch2str.count(ch) && ch2str[ch] != tmp) {
return false;
}
str2ch[tmp] = ch;
ch2str[ch] = tmp;
i = j + 1;
}
return i >= m;
}
};
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/word-pattern/solution/dan-ci-gui-lu-by-leetcode-solution-6vqv/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Solution {
public:
bool wordPattern(string pattern, string s) {
stringstream ss(s);
vector<string> strs;
string str;
while (ss >> str) {
strs.push_back(str);
}
unordered_map<char, string> char2Str;
unordered_map<string, char> str2Char;
if (pattern.size() != strs.size()) {
return false;
}
int N = pattern.size();
for (int i = 0; i < N; i++) {
if (char2Str.count(pattern[i])) {
if (char2Str[pattern[i]] != strs[i]) {
return false;
}
} else if (str2Char.count(strs[i])) {
if (str2Char[strs[i]] != pattern[i]) {
return false;
}
}
char2Str[pattern[i]] = strs[i];
str2Char[strs[i]] = pattern[i];
}
return true;
}
};
作者:jyj407
链接:https://leetcode.cn/problems/word-pattern/solution/zhong-gui-zhong-ju-290-dan-ci-gui-lu-by-hific/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
vector <string> split(const string &s, const char delimiter) {
vector <string> tokens;
string token;
istringstream token_stream(s);
while (getline(token_stream, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
class Solution {
public:
bool wordPattern(string pattern, string s) {
auto arr = split(s, ' ');
int m = pattern.size(), n = arr.size();
if (m != n) return false;
unordered_map<string, char> wordToPatternMap;
unordered_map<char, string> patternToWordMap;
for (int i = 0; i < n; ++i) {
if (wordToPatternMap.count(arr[i]) <= 0 && patternToWordMap.count(pattern[i]) <= 0) {
wordToPatternMap.emplace(pair{arr[i], pattern[i]});
patternToWordMap.emplace(pair{pattern[i], arr[i]});
continue;
} else {
if (wordToPatternMap[arr[i]] != pattern[i]) return false;
}
}
return true;
}
};
作者:Jasonkay
链接:https://leetcode.cn/problems/word-pattern/solution/c-liang-ge-hashbiao-ji-l-by-jasonkay-2h1b/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Solution {
public:
bool wordPattern(string pattern, string s) {
unordered_map<char,string> map;//考察哈希表
int i=0;
istringstream iss(s);//考察string的io操作
string word;
//需要pattern与s一一映射
while(iss>>word){
//利用istringstream可以按空格读取的特性
if(i==pattern.size())return 0;//两个字符串不一样长的话,s更长的情况
//不匹配情况1:a想同时映射dog和fish
/* 例子
"abba"
"dog cat cat fish"
*/
else if(map.find(pattern[i])!=map.end()&&map[pattern[i]]!=word)return 0;//考察哈希表的find操作和迭代器
//不匹配情况2:dog想同时映射a和b
/* 例子
"abba"
"dog dog dog dog"
*/
//考察哈希表的find_if操作及lambda表达式
else if(map.find(pattern[i])!=find_if(begin(map),end(map),[&](const pair<char,string>& p){return p.second==word;}))return 0;
else map[pattern[i]]=word;
++i;
}
if(i!=pattern.size())return 0;//两个字符串不一样长的话,s更短的情况
return 1;
}
};
作者:fenjue
链接:https://leetcode.cn/problems/word-pattern/solution/shi-yong-istringstreamhe-ha-xi-biao-hen-ka1qm/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Solution {
public:
bool wordPattern(string pattern, string s) {
unordered_map<char,string>p2s;
unordered_map<string,char>s2p;
int n=s.size(),i=0;
for(auto x: pattern){
if(i>=n) return false;
int j=i;
while(j<=n && s[j]!=' ') j++;
const string & check = s.substr(i,j-i);
if(p2s.count(x) && p2s[x]!=check) return false;
if(s2p.count(check) && s2p[check]!=x) return false;
p2s[x]=check;
s2p[check]=x;
i=j+1;
}
return i>=n;
}
};
stringstream
C++的输入输出分为三种:
(1)基于控制台的I/O
(2)基于文件的I/O
(3)基于字符串的I/O
https://www.cnblogs.com/gamesky/archive/2013/01/09/2852356.html
https://blog.csdn.net/wesigj/article/details/108959219