290. Word Pattern

Easy

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

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.

Example 1:

Input: pattern = "abba", str = "dog cat cat dog"
Output: true

Example 2:

Input:pattern = "abba", str = "dog cat cat fish"
Output: false

Example 3:

Input: pattern = "aaaa", str = "dog cat cat dog"
Output: false

Example 4:

Input: pattern = "abba", str = "dog dog dog dog"
Output: false

Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters that may be separated by a single space.

package leetcode.easy;

public class WordPattern {
@org.junit.Test
public void test() {
String pattern1 = "abba";
String str1 = "dog cat cat dog";
String pattern2 = "abba";
String str2 = "dog cat cat fish";
String pattern3 = "aaaa";
String str3 = "dog cat cat dog";
String pattern4 = "abba";
String str4 = "dog dog dog dog";
System.out.println(wordPattern(pattern1, str1));
System.out.println(wordPattern(pattern2, str2));
System.out.println(wordPattern(pattern3, str3));
System.out.println(wordPattern(pattern4, str4));
} public boolean wordPattern(String pattern, String str) {
String[] arrays = str.split(" ");
if (arrays.length != pattern.length()) {
return false;
} java.util.Map<String, Character> map = new java.util.HashMap<>();
for (int i = 0; i < pattern.length(); i++) {
if (map.containsKey(arrays[i]) && map.get(arrays[i]) != pattern.charAt(i)) {
return false;
} else if (!map.containsKey(arrays[i]) && map.containsValue(pattern.charAt(i))) {
return false;
} else {
map.put(arrays[i], pattern.charAt(i));
}
}
return true;
}
}

LeetCode_290. Word Pattern的更多相关文章

  1. [LeetCode] Word Pattern II 词语模式之二

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

  2. [LeetCode] Word Pattern 词语模式

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

  3. [LeetCode] Word Pattern

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

  4. Word Pattern | & II

    Word Pattern | Given a pattern and a string str, find if str follows the same pattern. Examples: pat ...

  5. 291. Word Pattern II

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

  6. leetcode面试准备: Word Pattern

    leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...

  7. Word Pattern

    ​package cn.edu.xidian.sselab.hashtable; import java.util.HashMap;import java.util.Map; /** *  * @au ...

  8. Word Pattern II 解答

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

  9. 【leetcode】290. Word Pattern

    problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...

随机推荐

  1. 使用fastjson 进行jsonObject转实体类对象

    使用fastjson 进行jsonObject转实体类对象   1 <dependency> 2 <groupId>com.alibaba</groupId> 3 ...

  2. Maximal Square II

    Description Given a 2D binary matrix filled with 0's and 1's, find the largest square which diagonal ...

  3. vs 在高分屏下开发 winform 配置

    一.窗体控件大小 第一种方法:使用网格避免整除误差 在选项中将Windows窗体设计器的LayoutMode(布局模式)改成SnapToGrid(对齐到网格),并将Default Grid Cell ...

  4. Greenplum failed segment的恢复方法--primary与mirror都可修复

    当在使用greenplum过程中有不当的操作时,可能会出现segment节点宕掉的情况(比如在greenplum运行的过程中停掉其中几台segment节点的服务器),通过下面的方法可以恢复segmen ...

  5. 常用命令备忘 lsof

    lsof命令 可以列出被进程所打开的文件的信息.被打开的文件可以是 1.普通的文件, 2.目录 3.网络文件系统的文件, 4.字符设备文件 5.(函数)共享库 6.管道,命名管道 7.符号链接 8.底 ...

  6. 记录一个奇怪的异常,无法还原此异常。 普通的Maven Java Web 项目

    项目 : 普通的Maven Java Web 项目 操作记录: 使用 Maven 构建项目,指令 tomcat7:run 无异常 但使用 eclipse 的 tomcat 运行项目,报此异常. 后面从 ...

  7. 模板 - 字符串 - KMP算法

    要先理解前缀函数的定义,前缀函数 \(\pi(i)\) 表示字符串 \(s[0,i]\) 的同时是其最长真前缀及最长真后缀的长度,简单来说就是这个 \(s[0,i]\) 首尾最长的重叠长度(不能完全重 ...

  8. Tkinter 之TopLevel顶级窗口

    一.参数说明 width  设置宽度 height  设置高度 background(bg) 设置背景颜色默认值由系统指定为了防止更新,可以将颜色值设置为空字符串 borderwidth(bd) 设置 ...

  9. phpMyadmin各个版本漏洞【转载】

    原作者:热爱网络安全的小菜狗 原文链接:phpMyadmin各版本漏洞 0x01 PREGREPLACEEVAL漏洞 影响版本:3.5.x < 3.5.8.1 and 4.0.0 < 4. ...

  10. Kubernetes 下零信任安全架构分析

    点击下载<不一样的 双11 技术:阿里巴巴经济体云原生实践> 本文节选自<不一样的 双11 技术:阿里巴巴经济体云原生实践>一书,点击上方图片即可下载! 作者 杨宁(麟童) 阿 ...