LeetCode(50)-Word Pattern
题目:
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.
思路:
- 题意:给定一个字符串a,这个字符串的字符给定了一种模式,然后给定一个字符串b其中把字符串用空格隔开,判断b是否和a的格式一样,a里面的字符相同,b也要相同,a不同,b也不同,同时位置也一样
- 采用的方法是a,转化为数组aa,bb,建立map,b根据空格,转化位数组,把a的存进map,判断map.containsKey(),包含去掉上一个重复值,添加新值,同时bb的相应位置元素也相同,不同的话,map.put,然后判断bb的元素和以前的都不同
-注意长度相同,同时都为空时返回true
代码:
public class Solution {
public boolean wordPattern(String pattern, String str) {
Map<Character,Integer> pp = new HashMap<Character,Integer>();
if(pattern == null && str == null){
return true;
}
char[] p = pattern.toCharArray();
String[] s = str.split(" ");
if(p.length != s.length){
return false;
}else{
for(int i = 0;i < p.length;i++){
if(pp.containsKey(p[i])){
int k = pp.get(p[i]);
pp.remove(p[i]);
pp.put(p[i],i);
if(!s[k].equals(s[i])){
return false;
}
}else{
pp.put(p[i],i);
for(int a = 0;a < i;a++){
if(s[a].equals(s[i])){
return false;
}
}
}
}
}
return true;
}
}
LeetCode(50)-Word Pattern的更多相关文章
- [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(单词模式)(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] 291. Word Pattern II 词语模式 II
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- leetcode 290. Word Pattern 、lintcode 829. Word Pattern II
290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...
- 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(一)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 STL
Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...
随机推荐
- git提交代码到github
前言:转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52117504 git提交代码到github 命令汇总: git init git ...
- 基于表单数据的封装,泛型,反射以及使用BeanUtils进行处理
在Java Web开发过程中,会遇到很多的表单数据的提交和对表单数据的处理.而每次都需要对这些数据的字段进行一个一个的处理就显得尤为繁琐,在Java语言中,面向对象的存在目的便是为了消除重复代码,减少 ...
- java设计模式---三种工厂模式
工厂模式提供创建对象的接口. 工厂模式分为三类:简单工厂模式(Simple Factory), 工厂方法模式(Factory Method)和抽象工厂模式(Abstract Factory).GOF在 ...
- [Python]django使用多进程连接msyql错误
问题 mysql 查询出现错误 error: (2014, "Commands out of sync; you can't run this command now") 查询 m ...
- Oracle WorkFlow(工作流)(二)
2.4消息(Message) 消息主要是为通知服务的,可以把消息当作通知的内容和类型.消息也属于一个单据类型,通知只能和同一个单据类型里的消息相关联. 每个消息可以有一个或多个属性和自己相联系,消息的 ...
- UI设计切忌墨守成规,但改变也须用数据说话
因为我提倡一种非标准的方法,Jon Galloway在一段评论里点了我的名: 年,他们很清楚怎么去填写这些表单.如果采用其他方法,用户会感到困惑,有些人还会落荒而逃(丢掉购物车,等等).Web表单很有 ...
- C++在单继承、多继承、虚继承时,构造函数、复制构造函数、赋值操作符、析构函数的执行顺序和执行内容
一.本文目的与说明 1. 本文目的:理清在各种继承时,构造函数.复制构造函数.赋值操作符.析构函数的执行顺序和执行内容. 2. 说明:虽然复制构造函数属于构造函数的一种,有共同的地方,但是也具有一定的 ...
- My Upload.com product submission has been approved
This morning I got to know that My product DB Query Analyzer had been appoved by upload.com, which ...
- 【Android 应用开发】Activity 状态保存 OnSaveInstanceState参数解析
作者 : 韩曙亮 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/38297083 一. 相关方法简介 1. 状态保存方法示例 p ...
- (NO.00002)iOS游戏精灵战争雏形(十一)
为了在子弹触碰到目标时做一些事情,我们必须要设置碰撞回调. 首先在MainScene.h的类接口中添加碰撞协议: @interface MainScene : CCNode <CCPhysics ...