- 浏览: 174724 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
比较简单的一道题目,可以先将str分割成字符数组,然后用哈希表来存储p中的字符以及对应字符数组中的字符串。其次我们还需要一个set来记录字符数组中的字符串是否已经被匹配过。代码如下:
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.
比较简单的一道题目,可以先将str分割成字符数组,然后用哈希表来存储p中的字符以及对应字符数组中的字符串。其次我们还需要一个set来记录字符数组中的字符串是否已经被匹配过。代码如下:
public class Solution { public boolean wordPattern(String pattern, String str) { String[] string = str.split("\\s"); HashMap<Character, String> hm = new HashMap<Character, String>(); Set<String> set = new HashSet<String>(); if(pattern.length() != string.length) return false; for(int i = 0; i < pattern.length(); i++) { if(hm.containsKey(pattern.charAt(i))) { if(!hm.get(pattern.charAt(i)).equals(string[i])) return false; } else { if(set.contains(string[i])) { return false; } else { hm.put(pattern.charAt(i), string[i]); set.add(string[i]); } } } return true; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 229Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 232You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 348Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 342Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 461Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 525Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 435Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 624Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 435The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 392Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 522Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 542Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 379All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 870Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 885Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 560Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 612Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 771Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 741You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 635For a undirected graph with tre ...
相关推荐
压缩包中包含Pattern Recognition Letters的LateX模板和word模板。
abstracted, characterized by a word pattern, and sent automatically to appropriate action points. This paper shows the flexibility of such a system in identifying known information, in finding who ...
Word Pattern 栈和队列 栈 Min Stack Valid Parentheses Longest Valid Parentheses Largest Rectangle in Histogram Evaluate Reverse Polish Notation Implement Stack using Queues 队列 Implement Queue using ...
官网就给了个sty文件,不知道咋弄的可以下载这个压缩包,直接添加内容
318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](./C++/maximum-product-of-word-lengths.cpp) [Python](./Python/maximum-product-of-word-...
示例 1:示例 2:示例 3:示例 4://单词分割存储//建立双向映射关系进行判断bool wordPattern(string pattern, strin
著名的《Head First Design Pattern》学习笔记,摘要这本书中的设计思路。由于书本过长,整理出笔记帮助回想起设计模式。文件是docx格式,只能由OFFICE Word 2007之后的版本打开,内附Visio类图文件。本文由个人整理...
struct pattern//pattern结构体,保存某个数字类别(0~9)的所有样品特征 { int number;//该手写数字样品个数 double feature[200][25];//各样品特征,每类手写数字最多有200个样品,每个样品有25个特征 };
可以将word,ppt转换成pdf格式, 1.安装《Apache_OpenOffice_4.1.5_Win_x86_install_zh-CN.exe》 2.将jar包加入到工程中 3.编写Java类,注意配置安装的位置 package com.han.office; import java.io.File; import ...
Pattern expression = Pattern.compile(regex); Matcher matcher = expression.matcher(source); TreeMap, Integer> myTreeMap = new TreeMap, Integer>(); int n = 0; Object word = null; Object num ...
word2vec-pride-vis 用最接近的 word2vec 模型词替换傲慢与偏见文本并可视化结果的黑客。 要使用它,您需要 Python 包 gensim、pattern.py。 此处提供本地 tsne.py。 IPython 笔记本查看器链接: : 如果您想运行它并...
Pattern Recognition Letters word模板 可供下载 Pattern Recognition Letters aims at rapid publication of concise articles of a broad interest in pattern recognition.
=======================setting_u.ini======================= [Settings] Custom=0 Extension= Description=Default File= Wildcard= First Line= ...Word Wrap=0 Auto Indent=1 Tab Indent=0 Common=0
URI范示 Pattern - 15 - Java调用定义 - 17 - 把404当作action来用 - 17 - 指派静态参数 - 17 - 变量和脚本 - 18 - 路由优先级 - 18 - 服务器静态资源 - 18 - staticDir: mapping - 18 - staticFile: mapping - 19 -...
字数访问者模式CS542设计模式2015年Spring项目分配编号04自述文件截止日期:4月22日,星期五。 提交日期:4月22日,星期五。 作者:Vinayak Subhash Pingale 电子邮件: 性能改进的理想选择有许多方法可以实现性能...
word=raw_input('input a word\n')\nurl='http://dict.youdao.com/search?q=%s'%word content=urllib.urlopen(url) pattern=re.compile("</h2>",re.DOTALL) result=pattern.search(content.read()).group() pattern2...
def getWordPattern(word): pattern = [] usedLetter={} count=0 for i in word: if i not in usedLetter: usedLetter[i]=count count+=1 pattern.append(str(usedLetter[i])) return '.'.join(pattern) ...
echo " Hello 123 " | groker -pattern= " %{WORD:word} %{WORD:number} " // output: { " number " : " 123 " , " word " : " Hello " } echo " 2015-06-16T01:47:07.665Z " | ./groker -pattern= " %{TIMESTAMP_...
使用时请将其转换成M文件格式,并且注意M文件中是英文状态,所以要特别注意word格式中的标点符号及提示符前面的空格是否是英文状态(在M文件中,如果字符不是英文状态,会出现波红色浪线的提示)。 如果文件中是函数...