WordPattern
Given a pattern and a string str, find if str follows the same pattern.
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:
patterncontains only lowercase alphabetical letters, andstrcontains words separated by a single space. Each word instrcontains only lowercase alphabetical letters.- Both
patternandstrdo not have leading or trailing spaces. - Each letter in
patternmust map to a word with length that is at least 1.
在LeeCode上有更简便的方法,使用了Map我对map还不够熟悉,所以在解决这个问题的时候,没有想起来用它
代码:
import java.util.Arrays;
public class WordPattern {
public static void main(String[] args) {
String pattern = "abab";
String str = "abc qwe abc qwe";
boolean compare = wordPattern(pattern, str);
if (compare) {
System.out.println("true");
} else {
System.out.println("false");
}
}
// 模式匹配
public static boolean wordPattern(String pattern, String str) {
// 转换为数组
String[] strings = str.split(" ");
char[] strings2 = pattern.toCharArray();
// 判断长度
if (strings2.length != strings.length) {
return false;
}
// 数组记录替换
int[] result1 = replaceString(strings);
int[] result2 = replaceChar(strings2);
Arrays.sort(result1);
Arrays.sort(result2);
if (!Arrays.equals(result1, result2)) {
return false;
}
return true;
}
// 数组替换
private static int[] replaceChar(char[] strings2) {
int[] array = new int[strings2.length];
for (int i = 0; i < array.length; i++) {
array[i] = 0;
}
for (int i = 0; i < strings2.length; i++) {
for (int j = 0; j < strings2.length; j++) {
char temp = strings2[i];
if (strings2[j] == temp) {
if (array[j] == 0 && temp == strings2[j]) {
array[j] = i + 1;
}
}
}
}
return array;
}
// 数组替换
private static int[] replaceString(String[] strings) {
int[] array = new int[strings.length];
for (int i = 0; i < array.length; i++) {
array[i] = 0;
}
for (int i = 0; i < strings.length; i++) {
String temp = strings[i];
for (int j = 0; j < strings.length; j++) {
if (temp.equals(strings[j]) && array[j] == 0) { // 此处注意 == 和
// equals的区别
array[j] = i + 1;
}
}
}
return array;
}
}
WordPattern的更多相关文章
- word-pattern(mock)
注意: // String要用equals,不然比较结果不对,会出bug// 使用String.split // boolean打印用 %b // abba 对应 cccc 也不行, 所以要用set ...
- [LeetCode] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
- Leetcode分类刷题答案&心得
Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...
- BUG-FREE-For Dream
一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...
- 全部leetcode题目解答(不含带锁)
(记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.) 88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...
- LeetCode 290 Word Pattern
Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- (String). Word Pattern
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- leetcode(一)Word Pattern
题目描述: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
- [LeetCode] Word Pattern
Word Pattern Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy Given a pattern and a st ...
随机推荐
- 退出Activity(转)
退出Activity 如何退出Activity?如何安全退出已调用多个Activity的Application? 退出activity 直接调用 finish () 方法 . //用户点击back键 ...
- Process32First 返回FALSE的原因
一般情况下是不会返回FALSE的,如果发生了,请检查: 1:系统为UNICODE的,一定要设置PROCESSENTRY32的dwSize为sizeof(PROCESSENTRY32)即可..
- dojo.publish 和 dojo.subscribe
原文链接:http://www.cnblogs.com/didi/archive/2010/06/13/1757894.html //dojo.publish 和 dojo.subscribe :d ...
- css整理-06 表和列表
表格式化 表布局 table, display:table caption, display: table-caption thead, display: table-header-group tbo ...
- DOM--4 响应用户操作和事件(1)
简介 事件:事件并不是以"on"开头的.例如,onclick引用的是一个对象的属性,click才是事件. 事件侦听器:当指定的事件发生时会执行的函数或方法. 事件注册:为DOM元素 ...
- Console.log,Window.alert,Document.write三者区别
1.Console.log不会阻断程序继续进行,在控制台可以看到测试结果. 2.Window.alert弹出框会阻断程序运行,在弹出框可以看到测试结果. 3.Document.write不会阻断程序继 ...
- 使用frameset时的target属性
http://blog.sina.com.cn/s/blog_8f82e8280101bwx9.html
- [转]HTML5本地存储——Web SQL Database
在HTML5 WebStorage介绍了html5本地存储的Local Storage和Session Storage,这两个是以键值对存储的解决方案,存储少量数据结构很有用,但是对于大量结构化数据就 ...
- LightOJ1017 Brush (III)(DP)
题目大概说一个平面上分布n个灰尘,现在要用一个宽w的刷子清理灰尘:选择一个起点,往水平线上扫过去这个水平线上的灰尘就消失了.问最多进行k次这样的操作能清理最多多少灰尘. 没什么的DP. 先按垂直坐标给 ...
- UpdatePanel的使用
一.UpdatePanel的结构 <asp:ScriptManager ID="ScriptManager1" runat="server" > & ...