Leetcode - Word Pattern
2016-05-05 18:24
316 查看
Question
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.Java Code
//将pattern中的每个字符与str中对应的单词构成一个<key, value>键值对,依次存入map中, //如果遇到与map中已有的映射关系相矛盾的键值对,则判断为假 public boolean wordPattern(String pattern, String str) { String[] value = str.split(" "); int len = pattern.length(); if(value.length != len) return false; HashMap<String, String> map = new HashMap<String, String>(); for(int i = 0; i < len; ++i) { String key = pattern.charAt(i) + ""; if(map.containsKey(key)) { if(!value[i].equals(map.get(key))) return false; }else { if(map.containsValue(value[i])) return false; map.put(key, value[i]); } } return true; }
相关文章推荐
- 微软Word 2007数学插件 Microsoft Math 提供下载
- Office Word九条常用技巧
- C#实现简单合并word文档的方法
- powershell操作word详解
- C#生成Word文档代码示例
- 用vbscript把 Word 文档保存为文本文件的代码
- 如何使用C#从word文档中提取图片
- C#根据Word模版生成Word文件
- C#采用OpenXml给Word文档添加表格
- C#实现合并多个word文档的方法
- C#采用OpenXml给word里面插入图片
- C#使用Word中的内置对话框实例
- C#生成word记录实例解析
- VB调用Word拼写检查功能实例
- C# WORD操作实现代码
- 比较全的一个C#操作word文档示例
- C#添加、读取Word脚注尾注的方法
- asp.net+Ligerui实现grid导出Excel和Word的方法
- jquery Moblie入门―hello world的示例代码学习
- asp.net中Word转Html的办法(不需要WORD组件)