LeetCode Word Pattern (模拟)
题意:
给出一个模式串pattern,再给出一个串str,问str的模板是否是pattern。
思路:
注意点:只要对于所有pattern[i]相同的i,str中对应的所有words[i]也必须相同,反过来,一个words[i]对应的也只有一个pattern[i]。
乱搞:
class Solution {
public:
bool wordPattern(string pattern, string str) {
set<string> sett[];
map<string,char> mapp;
string t;char c;int pos=;
for(int i=; i<str.size(); i++,pos++)
{
if(pattern.size()==pos) return false;
t="";
c=pattern[pos];
while(i<str.size()&&str[i]==' ') i++;
while(i<str.size()&&str[i]!=' ') t+=str[i++];
sett[c-'a'].insert(t);
if(sett[c-'a'].size()>) return false;
if(!mapp[t]) mapp[t]=c;
else if(mapp[t]!=c) return false;
}
if(pos!=pattern.size()) return false;
return true;
}
};
AC代码
用流:
class Solution {
public:
bool wordPattern(string pattern, string str) {
stringstream ss(str);
set<string> sett[];
map<string,char> mapp;
string t;char c;int pos=;
while(getline(ss,t,' '))
{
if(pattern.size()==pos) return false;
c=pattern[pos++];
sett[c-'a'].insert(t);
if(sett[c-'a'].size()>) return false;
if(!mapp[t]) mapp[t]=c;
if(mapp[t]!=c) return false;
}
return pos==pattern.size();
}
};
AC代码
LeetCode Word Pattern (模拟)的更多相关文章
- [LeetCode] Word Pattern II 词语模式之二
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
- [LeetCode] Word Pattern
Word Pattern Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy Given a pattern and a st ...
- Leetcode: Word Pattern II
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- Leetcode solution 291: Word Pattern II
Problem Statement Given a pattern and a string str, find if str follows the same pattern. Here follo ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)
翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...
- [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 ...
随机推荐
- oracle个人总结
oracle优化原则 1:insert 插入 (1):insert into /*+ append */ NOLOGGING 2: select 查询 (1):/*+ full(v) */ 全表查询 ...
- [工作技能]SVN
有的时候SVN上传txt文本文件,会报是bin文件的错误,解决方式是在.subversion文件夹下的config文件中加这么一句 *.txt = svn:mime-type=text/plain;s ...
- php验证用户名是否以字母开头与验证密码
验证用户名是否以字母开头与验证密码只能为数字和字母的组合代码三款三种常用验证函数 验证邮箱地址格式 验证密码只能为数字和字母的组合 验证用户名是否以字母开头代码哦,这是用户注册时或提交表单时会用的哦 ...
- HBase vs. BigTable Comparison - HBase对比BigTable
HBase vs. BigTable Comparison HBase is an open-source implementation of the Google BigTable architec ...
- jsp标签之<%%>和<%!%>
<%! %>中声明的是全局变量,不过写前面最好<% %>中声明的是局部变量.<%=%>一般表达式,输出某一变量的值.例如:<%! String totalSt ...
- jquery之getJSON方法获取中文数据乱码解决方法
最近公司做的东西要用到js,感觉js太繁琐,所以自己学起了jquery,发现jquery确实强大.在学到jquery ajax的时候(用的工具是eclipse),发现$.getJSON()方法请求服务 ...
- 青蛙的烦恼(dp好题)
有n片荷叶正好在一凸多边形顶点上 有一只小青蛙恰好站在1号荷叶的点 小青蛙可以从一片荷叶上跳到另外任意一片荷叶上 给出N个点的坐标N<800 求小青蛙想通过最短的路程遍历所有的荷叶一次且仅一次的 ...
- 前端必须掌握30个CSS3选择器
也许你已经学会了CSS的三个简单常用的选择器:#ID,.class,标签选择器,可是这些就足够了吗?随着CSS3的到来,作为前端开发者需要掌握下面三十个基本的选择器,这样才可以在平时开发中得心用手. ...
- 解决JavaScript中使用$.ajax方式提交数组参数
一般的,可能有些人在一个参数有多个值的情况下,可能以某个字符分隔的形式传递,比如页面上有多个checkbox: $.ajax{ url:"xxxx", data:{ p: &quo ...
- MySql 性能优化杂记
前一段时间接触MySql 服务器,关于查询忧化方面整理,优化主要唯绕几个工具函数 : show profiling , explain , 索引 , limit 如果上司抱怨服务器查询太慢,这时候 ...