翻译

给定一个模式,和一个字符串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)(*)的更多相关文章

  1. [LeetCode] 290. Word Pattern 单词模式

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  2. [LeetCode] 290. Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  3. 290 Word Pattern 单词模式

    给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循这种模式.这里的 遵循 指完全匹配,例如在pattern里的每个字母和字符串 str 中的每个非空单词存在双向单映射关系 ...

  4. leetcode 290. Word Pattern 、lintcode 829. Word Pattern II

    290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...

  5. 290. Word Pattern 单词匹配模式

    [抄题]: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...

  6. LeetCode 290. Word Pattern (词语模式)

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  7. LeetCode 290 Word Pattern

    Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...

  8. Leetcode 290 Word Pattern STL

    Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...

  9. [leetcode] 290. Word Pattern (easy)

    原题 思路: 建立两个哈希表,分别保存: 1 模式 :单词 2 单词 :是否出现过 水题 /** * @param {string} pattern * @param {string} str * @ ...

随机推荐

  1. 使用 SpiritManager 类管理在 XNA 游戏中的精灵(十四)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  2. 性能测试工具—Jmeter

    Jmeter视频教程: 在我要自学网搜索:关键字即可

  3. Your branch is ahead of 'origin/master' by 21 commits.

    当切换到主分支后,准备 git pull 拉取远程 master 分支时,提示本地主分支显示有 21 个commits 问题原因: 因为你修改了 local master 本地的主分支,可以选择以下方 ...

  4. 自己搭建一个记笔记的环境记录(leanote)

    一直在找一个开源的记笔记的软件,偶然看到leanote.竟然还是开源的,还是国人开发的果断mark了.自己在电脑上搭建了一个挺好玩的.可以记录一些不给别人看的小秘密. 下面是步骤记录,当然可以到官网上 ...

  5. 微信小程序--微信小程序tabBar不显示:缺少文件,错误信息:error:iconPath=

    1.list中的第一个tab的地址必须定义在pages 中 2.pagePath的地址一定要正确 正确写法是: "tabBar": { "color": &qu ...

  6. nyoj 题目17 单调递增最长子序列

    单调递增最长子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4   输入 ...

  7. Kotlin-Not enough information to infer parameter T in fun<T:View> findViewById(id: Int): T!

    代码改变世界 错误: Type inference failed : Not enough information to infer parameter T in fun<T:View> ...

  8. 爱之箭发射(las)

    爱之箭发射(las) 目描述 小海是弓道部的成员,非常擅长射箭(Love Arrow Shoot).今天弓道部的练习是要射一棵树.一棵树是一个nn个点n−1n−1条边的无向图,且这棵树的第ii个点有一 ...

  9. 基于SSM3框架FreeMarker自定义指令(标签)实现

    通过之前的Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解系列文章,我们已经成功的整合到了一起,这次大象将在此基础上对框架中的FreeMarker模板 ...

  10. tableView镶嵌加入CollectionView实现方法

    创建一个继承UICollectionView的类QHCollectionView在QHCollectionView.h中添加接口方法 @interface QHCollectionView : UIC ...