(String). 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 falsepublic class Solution { //if不用hashmap,更好的方法是设置头尾“指针”,保证一个指向当前的值
public boolean wordPattern(String pattern, String str) {
String[] strs = str.split(" ");
if (pattern.length() != strs.length)
return false;
Map<Character, String> map = new HashMap<Character, String>();
for (int i = 0; i < pattern.length(); i++) {
if (map.containsKey(pattern.charAt(i))
&& !(map.get(pattern.charAt(i))).equals(strs[i]))
return false;
if (!map.containsKey(pattern.charAt(i))
&& map.containsValue(strs[i]))
return false;
if (!map.containsKey(pattern.charAt(i))
&& !map.containsValue(strs[i]))
map.put(pattern.charAt(i), strs[i]);
}
return true;
}
}
(String). 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 ...
- Word Pattern | & II
Word Pattern | Given a pattern and a string str, find if str follows the same pattern. Examples: pat ...
- 291. Word Pattern II
题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...
- 290. Word Pattern
题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- Word Pattern
package cn.edu.xidian.sselab.hashtable; import java.util.HashMap;import java.util.Map; /** * * @au ...
- Word Pattern II 解答
Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
随机推荐
- 在 Windows 下部署 Vagrant
Vagrant 是一个可创建轻便.可复用的虚拟开发环境的跨平台工具,通过打包分可使所有开发成员使用统一的开发环境. 下面是我自己记录配置 Vagrant 的过程. 1.安装 VirtualBox 略. ...
- linux find
find 命令用于查找文件系统中的指定文件,其命令格式为:find 要查找的路径 表达式例如:find . -name 1.txt 在当前目录及其子目录下查找文件 1.txtfind ...
- PHP 输入流 php://input
在使用xml-rpc的时候,server端获取client数据,主要是通过php输入流input,而不是$_POST数组.所以,这里主要探讨php输入流php://input 对一php://in ...
- tcp之快速重传与恢复
本文为原创,转载请注明:http://www.cnblogs.com/gistao/ Background 写网络程序的都知道,tcp的窗口控制分为慢启动阶段和拥塞避免阶段,重传机制有快速重传/恢复和 ...
- 解决Oracle在scott用户下创建视图(VIEW)权限不足的方法
问题描述:在scott用户下创建视图的时候,报错:权限不足.(其他用户以此类推)解决方法: 以dba用户登录 sqlplus / as sysdba 赋予scott用户创建VIEW的权限 grant ...
- 在项目中导入MRC的文件时解决办法
1.由于在项目中要使用到第三方框架和其他的类的时候,而它用的是MRC的时候,其最简便的方法:完成从MRC到ARC的转换. 1.点击工程文件,进入到工程的设置里面. 2.看见Build Phases,就 ...
- April Fools Day Contest 2014
April Fools Day Contest 2014 A.C.H三道题目 ============================================================= ...
- Java中抽象类和接口
抽象类: 为什么要用抽象类? 1.父类的方法好多情况下是没有内容的.例如:USB是一个父类,里面的方法的函数体是可以不写,通过子类可以重写. 2.万一子类没有重写正确,是没有没有提示的.例如:父类中函 ...
- 【引】objective-c,3:关于block
原文参考博文: http://blog.devtang.com/2013/07/28/a-look-inside-blocks/ http://www.cnblogs.com/kesalin/arch ...
- 从DataTable获取Json数据
public string GetJson(DataTable dt){ JavaScriptSerializer jss=new JavaScriptSerializer(); jss.MaxJso ...