Given a pattern and a string str, find if str follows the same pattern.

Examples:

  1. pattern = "abba", str = "dog cat cat dog" should return true.
  2. pattern = "abba", str = "dog cat cat fish" should return false.
  3. pattern = "aaaa", str = "dog cat cat dog" should return false.
  4. pattern = "abba", str = "dog dog dog dog" should return false.

Notes:

  1. patterncontains only lowercase alphabetical letters, and str contains words separated by a single space. Each word in str contains only lowercase alphabetical letters.
  2. Both pattern and str do not have leading or trailing spaces.
  3. 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的更多相关文章

  1. word-pattern(mock)

    注意: // String要用equals,不然比较结果不对,会出bug// 使用String.split // boolean打印用 %b  // abba 对应 cccc 也不行, 所以要用set ...

  2. [LeetCode] Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...

  3. Leetcode分类刷题答案&心得

    Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...

  4. BUG-FREE-For Dream

    一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...

  5. 全部leetcode题目解答(不含带锁)

    (记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.)   88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...

  6. LeetCode 290 Word Pattern

    Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...

  7. (String). Word Pattern

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  8. leetcode(一)Word Pattern

    题目描述: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...

  9. [LeetCode] Word Pattern

    Word Pattern Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy Given a pattern and a st ...

随机推荐

  1. 用Feature的方式删除SharePoint2010的Page中重复的WebPart

    用Feature的方式删除SharePoint2010的Page中重复的WebPart. 代码如下所示: public class SupportCenterDuplicatedWebpartRemo ...

  2. ASCIIHexDecode,RunLengthDecode

    public static byte[] ASCIIHexDecode(byte[] data) { MemoryStream outResult = new MemoryStream(); bool ...

  3. cocoaPads 安装及出现Analyzing dependencies之后卡死解决方案

    1.安装 a. 查看源 gem sources -l b. 设置源: sudo gem sources -a http://ruby.taobao.org c. 删除源:sudo gem source ...

  4. JSHint配置详解

    Also available on Github JSHint配置详解 增强参数(Enforcing Options) 本类参数设为true,JSHint会产生更多告警. bitwise 禁用位运算符 ...

  5. 一定要学会paxos算法!

    paxos算法 http://blog.csdn.net/dellme99/article/details/14162159

  6. 去掉SrollView、GrdiView、ListView、ViewPager等滑动到边缘的光晕效果

    当我们使用SrollView.GrdiView.ListView.ViewPager带有滑动功能的组件时,滑动到边缘时总会出现类光晕效果.这是用于提示用户已经滑动到了组件的边缘,不能再滑动了,但有时候 ...

  7. nodejs随记02

    Basic认证 检查报文头中Authorization字段,由认证方式和加密值构成: basic认证中,加密值为username:password,然后进行Base64编码构成; 获取username ...

  8. Swift3.0语言教程使用占位符格式创建和初始化字符串

    Swift3.0语言教程使用占位符格式创建和初始化字符串 Swift3.0语言教程使用占位符格式创建和初始化字符串在很多的编程语言中都存在占位符,占位符就是为指定的内容占留一个位置.此功能一般在开发者 ...

  9. DELPHI与C#语法比较

    1.我做了三年的.NET,也是三个月前因为项目需要转的delphi整个过渡差不多要一周到两周.正常情况两周后就能熟悉delphi.delphi可以调整开发环境的,你把他的属性和解决方案窗口调成和你用V ...

  10. Bower In ASP.NET Core

    创建一个ASP.NET Core MVC项目的时候,会产生一个bower.json的文件,用于管理前段的js. NPM & Bower NPM主要运用于Node.js项目的内部依赖包管理,安装 ...