290. Word Pattern
来源:互联网 发布:win7 传奇3 数据库 编辑:程序博客网 时间:2024/06/10 01:10
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.
Credits:
Special thanks to @minglotus6 for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
用Map做就好
public class Solution { public boolean wordPattern(String pattern, String str) { char [] p = pattern.toCharArray(); String [] s = str.split(" "); HashMap<Character,String> maps = new HashMap<> (); if(p.length!=s.length)return false; for(int i = 0;i<p.length;i++){ if(!maps.containsKey(p[i])){ if(maps.containsValue(s[i]))return false; maps.put(p[i],s[i]); }else{ if(!maps.get(p[i]).equals(s[i]))return false; } } return true; }}
0 0
- 【leetcode】290. Word Pattern
- 【leetcode】290. Word Pattern
- 290. Word Pattern
- 290. Word Pattern
- 290. Word Pattern
- 290. Word Pattern
- 290. Word Pattern LeetCode
- 290. Word Pattern
- 290. Word Pattern
- 290. Word Pattern
- 290. Word Pattern
- 290. Word Pattern
- Leetcode 290. Word Pattern
- 290. Word Pattern
- 【LeetCode】290. Word Pattern
- 290. Word Pattern
- LeetCode-290. Word Pattern
- LeetCode *** 290. Word Pattern
- 299. Bulls and Cows
- BZOJ1901: Zju2112 Dynamic Rankings
- 将任意类型转为number类型
- xcode7编译老版本程序时提示错误的处理方法
- java核心技术笔记
- 290. Word Pattern
- Windows 基于udp的socket编程(课程代码基础上修改)
- 斜率游戏:8*8==13*5
- c++输入输出重定向
- PHP中require、include的区别
- python流程控制
- Sublime Text官方文档 中英文版本
- 38. Count and Say
- HDU1716 -- 全排列 -- STL -- next_permutation()