- 浏览: 183378 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 264Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 383Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 373Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 562Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 474Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 662Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 468The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 428Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 573Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 425All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 897Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 929Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 672Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 782You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
Match Word Pattern 匹配单词模式 Minimax极大 极小 N QueensN 皇后区 N Queens Math En 女王的数学 Power Sum功效和度 Rat In Maze 迷宫中的老鼠 Sudoku数独 Sum Of Subsets 子集总和 Word Search单词搜索 Bit ...
public boolean wordPattern(String pattern, String s) { String[] words = s.split(" "); Map, String> charMap = new HashMap(); Map, Character> wordMap = new HashMap(); if (words.length != pattern....
def wordPattern(pattern, s): words = s.split(' ') if len(pattern) != len(words): return False mapping = {} for p, w in zip(pattern, words): if p not in mapping and w not in mapping.values(): ...
《Elsevier模式识别信函(Pattern Recognition Letters)2020版论文模板解析与使用指南》 在学术界,撰写高质量的研究论文是每个科研人员必须掌握的技能之一。选择一个合适的论文模板能大大提高论文编写的效率和...
在给出的解决方案中,定义了一个`Solution`类,其中包含一个名为`wordPattern`的公共方法,该方法实现了上述逻辑。通过`map1`和`map2`这两个映射,我们可以有效地检查和建立字符与单词之间的双向对应关系。 总的来...
《Elsevier - 模式识别信函》(Pattern Recognition Letters) 是一本国际知名的学术期刊,专注于模式识别和机器学习领域的研究。此模板是为在该期刊发表论文的作者准备的,旨在提供一个标准的格式,确保文章符合...
【Elsevier的Word模板】是专门为向Elsevier期刊投稿的作者设计的一种文档格式,它简化了文章撰写和修改的过程,特别是在需要反复调整内容时,相比LaTeX提供了更大的便利性。这个模板包含了所有必要的元素,包括标题...
《模式识别信函》(Pattern Recognition Letters) 是一本在计算机科学和信息技术领域内享有盛誉的期刊,专注于模式识别和机器学习的研究成果。这个压缩包提供了该期刊的论文提交模板,方便作者按照统一的格式规范撰写...
9. **Word Pattern**:判断输入的字符串是否遵循给定的模式。可以通过建立两个哈希表,分别对应模式和字符串中的单词来实现。 10. **First Unique Character in a String**:找出字符串中第一个不重复的字符。可以...
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-...
Aspose.Words是Aspose公司提供的一款强大的API,它允许开发者在不依赖Microsoft Office的情况下,对Word文档进行创建、编辑和操作。本篇文章将深入探讨如何使用C#结合Aspose.Words实现批量替换文件夹下所有Word文件...
著名的《Head First Design Pattern》学习笔记,摘要这本书中的设计思路。由于书本过长,整理出笔记帮助回想起设计模式。文件是docx格式,只能由OFFICE Word 2007之后的版本打开,内附Visio类图文件。本文由个人整理...
<url-pattern>/servlet/DocumentHandleServlet</url-pattern> ``` ##### 3. 导出与下载 - **Servlet处理**:在`DocumentHandleServlet`类中处理请求,并填充数据。示例代码如下: ```java public class ...
5. **分支条件**: `(pattern1|pattern2)`表示匹配`pattern1`或`pattern2`。 6. **分组**: `(pattern)`用于创建一个捕获组,可以捕获匹配的子字符串,方便后续引用。 7. **后向引用**: `\1`、`\2`等用于引用前面...
在这个主题中,我们将探讨如何使用Python来根据关键词批量修改Word文档的样式。这个过程涉及到几个关键知识点,包括Python的文档处理库、正则表达式以及批量操作的概念。 首先,我们需要一个能够处理Word文档的...
PASCAL(Pattern Analysis, Statistical Modeling and Computational Learning)是用于计算机视觉和机器学习研究的一个数据集。在本案例中,可能是指PASCAL文本数据集,这是一个用于NLP任务的数据集,可能包含了各种...