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:
pattern
contains only lowercase alphabetical letters, andstr
contains words separated by a single space. Each word instr
contains only lowercase alphabetical letters.- Both
pattern
andstr
do not have leading or trailing spaces. - Each letter in
pattern
must 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 ...
随机推荐
- ASCIIHexDecode,RunLengthDecode
public static byte[] ASCIIHexDecode(byte[] data) { MemoryStream outResult = new MemoryStream(); bool ...
- Java利用Preferences设置个人偏好
Java利用Preferences设置个人偏好 Preferences的中文意思即偏好或喜好的意思,也就是说同一个程序在每次运行完后,可以通过Preferences来记录用户的偏好,下次启动时,程序会 ...
- Linux学习笔记(7)Linux常用命令之压缩解压命令
(1)gzip gzip命令用于压缩文件,英文原意为GNU zip,所在路径/bin/gzip,其语法格式为: gzip [文件] 压缩后的文件格式为.gz. 例:将/etc目录下的services文 ...
- Laravel之Service Container服务容器
managing class dependencies and performing dependency injection. Dependency injection is a fancy phr ...
- 分享Kali Linux 2016.2第48周虚拟机
分享Kali Linux 2016.2第48周虚拟机该虚拟机使用Kali Linux 2016.2第48周的64位镜像安装而成.基本配置如下:(1)该系统默认设置单CPU双核,内存为2GB,硬盘为50 ...
- springmvc导出excel并弹出下载框
https://my.oschina.net/aptx4869/blog/298507
- 51nod p1175 区间中第K大的数
1175 区间中第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 一个长度为N的整数序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有 ...
- [笔记] Duke - 统计预测
Duke大学富卡商学院(Fuqua school of business)的高级选修课. 全名:Statistical forecasting: notes on regression and tim ...
- requestAnimationFrame制作动画:旋转风车
在以往,我们在网页上制作动画效果的时候,如果是用javascript实现,一般都是通过定时器和间隔来实现的,出现HTML5之后,我们还可以用CSS3 的transitions和animations很方 ...
- ACM: Ubiquitous Religions-并查集-解题报告
Ubiquitous Religions Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Descript ...