#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <unordered_map>
#include <sstream>
#include <map>
using namespace std;
/*
问题:
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.
分析:bijection,双射
给定了模式,然后判断字符串是否符合该模式
主要是进行双射的判定。
读取模式串中当前字符,读取主串中当前分割后对应的字符串,
如果该映射还没有建立,则建立,如果映射已经建立,但是已经建立映射的值不等于当前
读取的主串中分割的对应字符串,则肯定不是
同理建立分割后字符串到主串中的匹配.
特殊情况:模式串为空或者主串为空都不符合
输入:
abba
dog cat cat dog
abba
dog cat cat fish
aaaa
dog cat cat dog
abba
dog dog dog dog
输出:
true
false
false
false
leecode:https://leetcode.com/problems/word-pattern/?tab=Solutions 解法
将<char , int> , <string , int>都建立映射,分别映射到下标,
对于当前给定的字符和字符串,如果都存在,判断其映射的下标是否相同
如果其中一个不存在,另一个映射能够找到,返回int,而另外一个返回end()导致不等,返回实际不等是正确的
如果两个都不存在,都建立该映射
举例:
abba
dog cat cat dog
<a,1>,<b,2>,遇到a返回下标1
<dog,1>,<cat,2>,遇到cat返回下标为1,两者映射到同一个地方相同,说明是一直的
abba
dog dog dog dog
<a,1>,查找不到,返回末尾
<dog,1>,返回迭代器,两者不等
关键:
1
leecode:https://leetcode.com/problems/word-pattern/?tab=Solutions 解法
将<char , int> , <string , int>都建立映射,分别映射到下标,
对于当前给定的字符和字符串,如果都存在,判断其映射的下标是否相同
如果其中一个不存在,肯定不是
如果两个都不存在,都建立该映射
2 对于带有空格的字符串,可以先转化为输入流,然后读取单词,牛逼,
不需要切分
*/
class Solution {
public:
bool wordPattern(string pattern, string str) {
if(pattern.empty() || str.empty())
{
return false;
}
int size = pattern.size();
string word;
unordered_map<char , int> charToInt;
unordered_map<string ,int> strToInt;
istringstream instream(str);//转化为输入流
int i = 0;
int charIndex = 0;
int strIndex = 0;
char ch;
while(instream >> word)
{
//说明输入的单词个数太多
if(i >= size)
{
return false;
}
ch = pattern.at(i);
//如果两个都无法找到,新建映射
if(charToInt.find(ch) == charToInt.end() && strToInt.find(word) == strToInt.end())
{
charToInt[ pattern.at(i) ] = strToInt[ word ] = i;
}
//两者中有一个找不到,肯定不是
else if(charToInt.find(ch) == charToInt.end() || strToInt.find(word) == strToInt.end())
{
return false;
}
//两者都能找到,但是值不同,也肯定不是
else if(charToInt[ch] != strToInt[word])
{
return false;
}
i++;
}
return i == size;//判断最终结果是否长度相同
}
};
void process()
{
const int MAXSIZE = 1024;
char pattern[MAXSIZE];
char str[MAXSIZE];
Solution solution;
while(gets(pattern) )
{
gets(str);
string sStr(str);
string sPattern(pattern);
bool result = solution.wordPattern(sPattern , sStr);
if(result)
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
}
}
int main(int argc , char* argv[])
{
process();
getchar();
return 0;
}
/*
vector<string> split(string str ,string splitStr)
{
int beg = 0;
int pos = str.find(splitStr , beg);
vector<string> result;
string partialStr;
while(string::npos != pos)
{
partialStr = str.substr(beg , pos - beg);
if(!partialStr.empty())
{
result.push_back(partialStr);
}
beg = pos + splitStr.length();
pos = str.find(splitStr , beg);
}
partialStr = str.substr(beg , pos - beg);
if(!partialStr.empty())
{
result.push_back(partialStr);
}
return result;
}
bool check(string& pattern , vector<string>& words)
{
if(pattern.empty() || words.empty() || pattern.length() != words.size())
{
return false;
}
int size = words.size();
unordered_map<char , string> visited;
char ch;
for(int i = 0 ; i < size ; i++)
{
ch = pattern.at(i);
if(visited.find(ch) != visited.end())
{
if(visited[ch] != words.at(i))
{
return false;
}
}
else
{
visited[ch] = words.at(i);
}
}
unordered_map<string, char> visited2;
for(int i = 0 ; i < size ; i++)
{
ch = pattern.at(i);
if(visited2.find(words.at(i)) != visited2.end())
{
if(visited2[words.at(i)] != ch)
{
return false;
}
}
else
{
visited2[words.at(i)] = ch;
}
}
return true;
}
//
bool wordPattern2(string pattern, string str) {
if(pattern.empty() || str.empty())
{
return false;
}
vector<string> words = split(str , string(" "));
bool result = check(pattern , words);
return result;
}
*/
leecode 解题总结:290. Word Pattern
最新推荐文章于 2019-03-27 22:59:38 发布