LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)
翻译
给定一个模式,和一个字符串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;
}
updated at 2016/09/17
一点半了,大半夜的不睡觉~
没看之前写的这篇博客。只是思想应该是几乎相同的~ pattern方面还是一样,str的话就先构造出一个String数组。然后在HashMap中存储和推断String。而不是Char了。
public boolean wordPattern(String pattern, String str) {
ArrayList s0 = getArrayOrder(pattern);
ArrayList s1 = getArrayOrder2(getArrayFromString(str));
if (s0.size() != s1.size())
return false;
for (int i = 0; i < s0.size(); i++) {
if (s0.get(i) != s1.get(i)) {
return false;
}
}
return true;
}
private ArrayList getArrayOrder(String str) {
HashMap<Character, Integer> strM = new HashMap<>();
int index = 0;
ArrayList order = new ArrayList(str.length());
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (strM.containsKey(c)) {
order.add(strM.get(c));
} else {
strM.put(c, index);
order.add(index);
index += 1;
}
}
return order;
}
private ArrayList<String> getArrayFromString(String str) {
ArrayList<String> arrayList = new ArrayList<>();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ' && !builder.toString().equals("")) {
arrayList.add(builder.toString());
builder = new StringBuilder();
} else if (i == str.length() -1) {
builder.append(str.charAt(i));
arrayList.add(builder.toString());
builder = null;
} else {
builder.append(str.charAt(i));
}
}
return arrayList;
}
private ArrayList getArrayOrder2(ArrayList<String> arrayList) {
HashMap<String, Integer> strM = new HashMap<>();
int index = 0;
ArrayList order = new ArrayList(arrayList.size());
for (int i = 0; i < arrayList.size(); i++) {
String s = arrayList.get(i);
if (strM.containsKey(s)) {
order.add(strM.get(s));
} else {
strM.put(s, index);
order.add(index);
index += 1;
}
}
return order;
}
代码
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;
}
};
LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)的更多相关文章
- [LeetCode] 290. Word Pattern 单词模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] 290. Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- 290 Word Pattern 单词模式
给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循这种模式.这里的 遵循 指完全匹配,例如在pattern里的每个字母和字符串 str 中的每个非空单词存在双向单映射关系 ...
- leetcode 290. Word Pattern 、lintcode 829. Word Pattern II
290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...
- 290. Word Pattern 单词匹配模式
[抄题]: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
- LeetCode 290. Word Pattern (词语模式)
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- LeetCode 290 Word Pattern
Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- Leetcode 290 Word Pattern STL
Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...
- [leetcode] 290. Word Pattern (easy)
原题 思路: 建立两个哈希表,分别保存: 1 模式 :单词 2 单词 :是否出现过 水题 /** * @param {string} pattern * @param {string} str * @ ...
随机推荐
- sql注入过滤了#,--+怎么办
题目是NCTF2018的web题目 第一段是错误的思路,第二段是晚上有思考后发现的直接看第二段吧. ① ?id=1'会直接出来报错提示. 猜测使用单引号保护id. 另外一打空格就提示you hacke ...
- Python学习-day11 RabbitMQ Redis
这次文章包含两个内容: 1.RabbitMQ使用 2.Redis基础操作 代码部分为练习笔记和作业 概念部分转自Alex老师 RabbitMQ 安装 http://www.rabbitmq.com/i ...
- [oldboy-django][2深入python] orm中auto_now =True, antu_now_add=True的应用
DateTimeField.auto_now 这个参数的默认值为false,设置为true时,能够在保存该字段时,将其值设置为当前时间,并且每次修改model,都会自动更新.因此这个参数在需要存储“最 ...
- 欧拉路&&欧拉回路 概念及其练习
欧拉路: 如果给定无孤立结点图G,若存在一条路,经过图中每边一次且仅一次,这条路称为欧拉路: 如果给定无孤立结点图G,若存在一条回路,经过图中每边一次且仅一次,那么该回路称为欧拉回路. 存在欧拉回路的 ...
- cf 853 B Jury Meeting [前缀和]
题面: 传送门 思路: 看完题目以后,首先有一个结论:每个人都是先去到首都,等待开会,开会结束以后再一个个走掉 而且这道题只有去首都和离开首都的机场 因此考虑计算去首都的飞机的前缀最小花费,以及离开首 ...
- BZOJ5306 [HAOI2018]染色 【组合数 + 容斥 + NTT】
题目 为了报答小 C 的苹果, 小 G 打算送给热爱美术的小 C 一块画布, 这块画布可 以抽象为一个长度为 \(N\) 的序列, 每个位置都可以被染成 \(M\) 种颜色中的某一种. 然而小 C 只 ...
- bzoj2115【WC2011】XOR
题意:http://www.lydsy.com/JudgeOnline/problem.php?id=2115 sol :首先考虑处理出DFS树,那么树上的所有非树边可以构成一个简单环 因为所有不在 ...
- UIAlertController 实现kvo实现mes文字设置
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新提示" message:[NS ...
- 学习 表单验证插件validate
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---2
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下: <Linux命令行与shell脚本 ...