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. tree view

    <TreeView x:Name="treeParameter" Width=" Margin="11,6,11,6" ItemsSource= ...

  2. 面向对象中this问题

    this什么时候会出错? 注意:其中var _this = this 的巧妙用法. 1.定时器: <script type="text/javascript"> //如 ...

  3. Eclipse导出可执行Java工程/可执行Jar文件(包含第三方Jar包)

    1. 首先,右键你的Java工程,选择Export,在Java文件夹下选择Runnable JAR file,如下图所示: 2. 选择Runnable JAR file后,会弹出如下所示的对话框,选择 ...

  4. hdu1003 dp

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1003 #include<cstdio> #include<algorit ...

  5. C#与mysql做ASP.NET网页数据库查询速度测试

    两种方法是:1,使用mysql数据库的存储过程:2,C#编码,做网页后台与mysql数据库连接,前台测试显示测试过结果下面我将分别讲解两种方法的具体实现. 1,使用mysql数据库的存储过程插入万条大 ...

  6. web开发的基础知识:http请求

    引用自:http://blog.csdn.net/yefan2222/article/details/6198098 http://baike.baidu.com/view/1628025.htm?f ...

  7. PHP历程(PHP与MYSQL数据库之间连接、创建和关闭)

    <?php define('WXLEVELS_DB_HOST','127.0.0.1'); //服务器 define('WXLEVELS_DB_USER','root'); //数据库用户名 d ...

  8. JavaScript定时器

    定时器 开启定时器 Setinterval间隔型    每隔一段时间重复的执行 SetTimeout延时型   只执行一次 两种定时器的区别 <!DOCTYPE html> <htm ...

  9. get请求

    写在前面的话 XMLHttpRequest对象的open方法的第一个参数为request-type,取值可以为get或post.本篇介绍get请求. get请求的目的,主要是为了获取数据.虽然get请 ...

  10. putty ssh连接老断

    有两种方法来解决:一. 配置客户端 1 在 linux下的ssh命令:vim /etc/ssh/ssh_config 然后找到里面的ServerAliveInterval 参数,如果没有你同样自己加一 ...