(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 ...
随机推荐
- robotframework接口测试初探2
python这个requests模块常被用来测试接口.使用RequestLibrary库测试之前,先来看下这个模块是怎样使用的 最简单的调用是 r=requests.get("http:// ...
- Character Timing for T=0
The minimum interval between the leading edges of the start bits of two consecutive characters sent ...
- 【转】构建C1000K的服务器(1) – 基础
原文来自 ideawu 构建C1000K的服务器(1) – 基础 著名的 C10K 问题提出的时候, 正是 2001 年, 到如今 12 年后的 2013 年, C10K 已经不是问题了, 任何一个普 ...
- JS函数输出圆的半径和面积
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Emgu学习手册
作为opencv的c#封装库.emgu可以满足基本的图像处理功能,经过测试,效果还可以,主要用于windows窗体应用程序的开发,或者wpf,你可以用来做ocr,也可以用来做人脸识别或者可以用来做定位 ...
- MyEclipse使用前优化与配置
全局优化 1 设置默认编码方式 首选项> General > Workspace > GBK改成UTF-8 2 设置默认文件默认打开方式 首选项> General > ...
- LintCode Two Sum
1. 数组numbers == null 及numbers.length == 0, 而不是用numbers[] 2. HashMap<Integer, Integer>而不是<in ...
- PL_SQL导入数据库数据
首先用pl/sql将数据批量导出或者全部导出具体操作如下 点击工具 ----->导出数据---->SQL插入 导出的数据格式如下: prompt PL/SQL Developer im ...
- 2015.10.15night
#include<stdio.h> main() { int x,y; scanf("%d",&x); if(x>0)y=1; else {if(x< ...
- STM32学习笔记——OLED屏
STM32学习笔记--OLED屏 OLED屏的特点: 1. 模块有单色和双色可选,单色为纯蓝色,双色为黄蓝双色(本人选用双色): 2. 显示尺寸为0.96寸 3. 分辨率为128*64 4. ...