Leetcode 290. Word Pattern
来源:互联网 发布:大数据前沿技术应用 编辑:程序博客网 时间:2024/06/06 08:51
Leetcode 290. Word Pattern
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.
<pre name="code" class="java"> public boolean wordPattern(String pattern, String str) { boolean flag = false; int length1 = pattern.length(); String[] str1 = str.split(" "); int length2 = str1.length; int count = 0; HashMap map1 = new HashMap(); HashMap map2 = new HashMap(); int[] array1= new int[length1]; int[] array2= new int[length2]; if(length1 != length2) flag = false; else{ for(int i = 0; i < length1; i++){ if(map1.containsKey(String.valueOf(pattern.charAt(i)))){ array1[i] = map1.get(String.valueOf(pattern.charAt(i))); } else{ map1.put(String.valueOf(pattern.charAt(i)), i); array1[i] = i; } if(map2.containsKey(str1[i])){ array2[i] = map2.get(str1[i]); } else{ map2.put(str1[i], i); array2[i] = i; } } for(int i = 0; i < length1; i++){ if(array1[i] == array2[i]){ count ++; } } if(count == length1){ flag = true; } } return flag; }
1 0
- 【leetcode】290. Word Pattern
- 【leetcode】290. Word Pattern
- 290. Word Pattern LeetCode
- Leetcode 290. Word Pattern
- 【LeetCode】290. Word Pattern
- LeetCode-290. Word Pattern
- LeetCode *** 290. Word Pattern
- leetcode 290. Word Pattern
- LeetCode 290. Word Pattern
- LeetCode 290. Word Pattern
- [leetcode] 290. Word Pattern
- 【LeetCode】290. Word Pattern
- LeetCode-290.Word Pattern
- Leetcode 290. Word Pattern
- leetcode:290. Word Pattern
- 290.[Leetcode]Word Pattern
- [Leetcode]290. Word Pattern
- LeetCode 290. Word Pattern
- Linux kernel 的 sendfile 是如何提高性能的
- IP地址0.0.0.0是什么意思?
- 上一周的知识点总结以及思路的理解
- Spring+MyBatis多数据源配置实现
- c++调用python发送邮件
- Leetcode 290. Word Pattern
- GIT工作流程/步骤/基本命令
- 在Myeclipse中添加代码提示功能
- Linux实用命令
- 某大数据平台挂载硬盘过程
- [leetcode] 11. Container With Most Water
- Android-ListView 学习笔记
- 数学问题:完数
- Universal-Image-Loader源码阅读(4)-utils/ImageSizeUtils