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 ...
随机推荐
- pythonchallenge之C++学习篇-01
字符处理时每个语言都具备的一种功能,其中还有一些语言因此出名,比如perl,shell,还有一些函数式的编程语言 C语言中的字符串与数组和指针联系的比较紧密,因此可以这样生命字符串*p="h ...
- Nginx日志定时切割脚本
nginx的日志文件如果你不处理,将变得越来越大,我们可以写一个nginx日志切割脚本来自动切割日志文件. 第一步就是重命名日志文件,不用担心重命名后nginx找不到日志文件而丢失日志.在你未重新打开 ...
- JavaScript设计模式——前奏
Function.prototype.method = function(name,fn){ this.prototype[name] = fn; } var Anim = function(){ / ...
- loopback 04
数据库相关 关系定义 relationship 定义关系之后的使用 relations: { "images": { "type": "hasMany ...
- no-jquery 02 DOM
DOM Manipulation Creating Elements // IE 5.5+ document.createElement('div'); Inserting Elements Befo ...
- sql日期格式化
0 或 100 (*) 默认值 mon dd yyyy hh:miAM(或 PM) 1 101 美国 mm/dd/yyyy ...
- 用脚本完成mysql工作
1. 用mysql -e在脚本中执行mysql的sql语句 #!/bin/bash #simple mysql shell usage logtime=`date "+%Y-%m-%d&qu ...
- Spring Data JPA 查询方法支持的关键字
Table 2.3. Supported keywords inside method names Keyword Sample JPQL snippet And findByLastnameAndF ...
- R语言常用命令
data() 列出当前已安装包中所有可用的实例数据集 help("name") 查看帮助文档 summary()
- header元素
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...