Leetcode 205 Isomorphic Strings的进阶版

这次是词组字符串和匹配字符串相比较是否一致

请使用map来完成模式统计

 class Solution {
public:
bool wordPattern(string pattern, string str) {
map<char,int> mp;
map<string,int> ms;
vector<int> p,s;
int cnt = ;
for(string::size_type i = ; i < pattern.size(); ++i){
if(mp.find(pattern[i]) == mp.end()){
mp[pattern[i]] = cnt++;
p.push_back(mp[pattern[i]]);
}
else p.push_back(mp[pattern[i]]);
}
int a = , b = ;
cnt = ;
while((b = str.find(" ", a)) != string::npos){
string t = str.substr(a, b - a);
if(ms.find(t) == ms.end()){
ms[t] = cnt++;
s.push_back(ms[t]);
}
else s.push_back(ms[t]);
a = b + ;
}
b = str.size();
string t = str.substr(a, b - a);
if(ms.find(t) == ms.end()){
ms[t] = cnt++;
s.push_back(ms[t]);
}
else s.push_back(ms[t]);
if(p.size() != s.size()) return false;
for(vector<int>::size_type i = ; i < s.size(); ++i){
if(s[i] != p[i]) return false;
}
return true;
}
};

Leetcode 290 Word Pattern STL的更多相关文章

  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 、lintcode 829. Word Pattern II

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

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

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

  4. LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)

    翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...

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

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

  6. LeetCode 290 Word Pattern

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

  7. Java [Leetcode 290]Word Pattern

    题目描述: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...

  8. leetcode 290 Word Pattern(map的应用)

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

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

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

随机推荐

  1. rhel7防止开机破密码

    在/etc/grub.d/00_header文件结尾加入  cat <<EOF set  superusers="qin"#用户名称可以更加自身编辑 password ...

  2. ZT “樱花小萝莉”走红网络 网友:好想生个女儿

    “樱花小萝莉”走红网络 网友:好想生个女儿 投递人 itwriter 发布于 2014-04-02 17:39 评论(3) 有717人阅读  原文链接  [收藏]  « » 近日,一组被网友亲切地称呼 ...

  3. linux学习之——基础命令

    Linux体系基础命令: Linux是一个命令行组成的操作体系!精华在命令行,岂论图形界面成长到什么水平这个原理是不会变的,Linux命令有许多壮大的效用:从简单的磁盘操作.文件存取.到举办庞大的多媒 ...

  4. NVelocity 表格行奇偶样式变换

    #foreach($test in $tests) #even <tr class="Test1"> #odd <tr class="Test2&quo ...

  5. mysql提示2002错误的解决方法

    前两天,负责的一个项目出现问题,总是提示"SQLSTATE[HY000] [2002] 由于目标计算机积极拒绝,无法连接",由于负责服务器的同事联系不到,我无法登陆服务器查看原因, ...

  6. SQL Server跨服务器的数据库迁移

    1. 使用sql server task中back up 任务,保存为*.bak 文件. 2. 在另一个server中restore database,如果已经存在这个database,会覆盖之前的数 ...

  7. 读取iOS通讯录

    首先导入头文件 #import <AddressBook/AddressBook.h> 获取权限 读取通讯录 - (void)loadPerson { ABAddressBookRef a ...

  8. 使用jquery的trigger方法优化页面代码

    我们做页面级联的时候经常会用到ajax处理数据,会为下拉菜单编写change事件. //城市和区域联动 $("#City").change(function () { var ci ...

  9. wexinjs 调用

    public class Utils    {       static string appid = GetAppSettingValue("appid");       sta ...

  10. BST、B树、B+树、B*树

    一. BST BST即二叉搜索树Binary Search Tree(又叫二叉排序树Binary Sort Tree).它有以下特点: 所有非叶子结点至多拥有两个儿子(Left和Right): 所有结 ...