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 ...
随机推荐
- 建库和表的脚本.sql
1.一直都记不太清楚,需要新建一个数据库和表的脚本是怎样的,恰巧今天翻到了,特地记录下来,希望以后用的时候记住吧! create database testdb00; use testdb00; cr ...
- Mysql多表关联删除操作
直接看Sql即可: ;
- Huffman树与编码的简单实现
好久没写代码了,这个是一个朋友问的要C实现,由于不会C,就用JAVA写了个简单的.注释掉的代码属性按照原来朋友发的题里带的参数,发现没什么用就给注释掉了. package other; import ...
- (置顶)js实现超过页面一屏后,点击图标滚动到页面顶部top
<script type="text/javascript">$(document).ready(function() { var ScrolltoTop = $ ...
- 使用AlarmManager定时更换壁纸----之一
import android.os.Bundle;import android.app.Activity;import android.app.AlarmManager;import android. ...
- js控制html元素的readonly属性
html元素假设为只读,那么其readonly="readonly",我们现在想通过js来改变readonly属性为可以输入. 初始时,两个输入框都是只读.点击change按钮后, ...
- [Js]弹性运动
描述:像弹簧一样左右弹动,最后缓慢停下来 一.加减速运动 1.加速运动 var iSpeed=0;iSpeed++; 速度越来越快,最后冲出去 2.减速运动 var iSpeed=20;iSpeed- ...
- POJ 2632 Crashing Robots 模拟 难度:0
http://poj.org/problem?id=2632 #include<cstdio> #include <cstring> #include <algorith ...
- python错误集锦
1.lt与list等同,不能作为变量名 2.中文路径名:os.path.normcase(filepath) 如果遇到 ascii码在路径某处不能转换, 那么 filepath.encode('gbk ...
- 判断comboBox是否选对了绑定的数据库中的项
实现: comboBox1下拉列表已绑定数据库,将选中的项保存到数据库时,判断是否已选中下拉列表里的项 如果没选中,或者输入了其他的值,和已绑定的数据不匹配,出现提示框 按钮的点击事件中: strin ...