题目描述:

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:

  1. pattern = "abba", str = "dog cat cat dog" should return true.
  2. pattern = "abba", str = "dog cat cat fish" should return false.
  3. pattern = "aaaa", str = "dog cat cat dog" should return false.
  4. 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.

解题思路:

使用HashMap来做。

代码如下:

public class Solution {
public boolean wordPattern(String pattern, String str) {
if(pattern == null || str == null)
return false;
String[] words = str.split(" ");
if(pattern.length() != words.length)
return false;
Map<Character, String> map = new HashMap<Character, String>();
for(int i = 0; i < pattern.length(); i++){
char t = pattern.charAt(i);
if(map.containsKey(t)){
if(!words[i].equals(map.get(t)))
return false;
} else {
if(map.containsValue(words[i]))
return false;
map.put(t, words[i]);
}
}
return true;
}
}

  

Java [Leetcode 290]Word Pattern的更多相关文章

  1. [LeetCode] 290. Word Pattern 单词模式

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  2. leetcode 290. Word Pattern 、lintcode 829. Word Pattern II

    290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...

  3. [LeetCode] 290. Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  4. LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)

    翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...

  5. LeetCode 290. Word Pattern (词语模式)

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  6. LeetCode 290 Word Pattern

    Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...

  7. Leetcode 290 Word Pattern STL

    Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...

  8. leetcode 290 Word Pattern(map的应用)

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  9. [leetcode] 290. Word Pattern (easy)

    原题 思路: 建立两个哈希表,分别保存: 1 模式 :单词 2 单词 :是否出现过 水题 /** * @param {string} pattern * @param {string} str * @ ...

随机推荐

  1. Asp.Net原理Version2.0

    有些部分被省略,可以看看Asp.Net原理Version1.0 Asp.Net原理Version3.0_页面声明周期

  2. 2014 Multi-University Training Contest 10

    官方解题报告:http://blog.sina.com.cn/s/blog_6bddecdc0102v01l.html A simple brute force problem. http://acm ...

  3. AlphaToCoverage solution

    After msaa output the alpha in ps remove clip in ps in blendstate add AlphaToCoverageEnable = TRUE - ...

  4. 把eclipse"中文版"变成"英文版"

    在Eclipse.exe当前路径下,直接新建快捷方式,右键属性,添加如下参数即可 eclipse.exe -nl en

  5. SQL语句备忘

    SELECT beatid,COUNT(d.id) dongnicount FROM `bed_beat_dongni` d INNER JOIN bed_beat b on b.id = d.bea ...

  6. Maintainable HashCode and Equals Using Apache Commons

    Java hashCode and equals methods can be tricky to implement correctly. Fortunately, all majors IDEs ...

  7. POJ 3273

    Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12122   Accepted: 4932 ...

  8. Javascript通过className选择元素

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  9. ios APP屏幕快照尺寸

    苹果上传APP审核需要上传APP屏幕快照,分别有3.5寸,4寸,4.7寸,5.5寸,ipad五种图片,对应尺寸大小: 3.5寸:横坚屏 640*960 或960*640 4寸:横坚屏 640*1036 ...

  10. 天气API整理,返回的数据格式为json对象

    中国天气的API惨遭封杀,不得已更换其他的API使用吧. 这里大部分API来自APP的数据包截取或者反汇编,所以各开发使用者不得用于商业用途,否则后果自负! 1.金山网址导航 来源:http://ww ...