leetcode面试准备: Word Pattern

1 题目

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, and str contains words separated by a single space. Each word in str contains only lowercase alphabetical letters.

Both pattern and str do not have leading or trailing spaces.

Each letter in pattern must map to a word with length that is at least 1.

接口: public boolean wordPattern(String pattern, String str)

2 思路

题意

通过pattern来匹配单词,很容易想到HashMap思想。

注意

  1. pattern的长度,不对应str拆分单词后的长度。
  2. 一个pattern字母和唯一一个字符串相互对应。

复杂度: Time:O(n) Space: O(n)

3 代码

public boolean wordPattern(String pattern, String str) {
String[] words = str.split("\\s", -1);
int len = words.length;
if (pattern.length() != len) { // 长度不等直接false,防止后面数组越界
return false;
}
Map<Character, String> map = new HashMap<>(26);
for (int i = 0; i < len; i++) {
Character c = pattern.charAt(i);
if (map.containsKey(c)) {
if (!map.get(c).equals(words[i]))
return false;
} else {
if (map.containsValue(words[i])) { // 字母和字符串一一对应。
return false;
}
map.put(c, words[i]);
}
}
return true;
}

4 总结

简单题,注意细节。一次AC。

5 参考

leetcode

leetcode面试准备: Word Pattern的更多相关文章

  1. Leetcode solution 291: Word Pattern II

    Problem Statement Given a pattern and a string str, find if str follows the same pattern. Here follo ...

  2. 【leetcode】290. Word Pattern

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

  3. 【一天一道LeetCode】#290. Word Pattern

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. Baozi Leetcode Solution 290: Word Pattern

    Problem Statement Given a pattern and a string str, find if str follows the same pattern. Here follo ...

  5. 【LeetCode】290. Word Pattern 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. LeetCode算法题-Word Pattern(Java实现)

    这是悦乐书的第202次更新,第212篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第68题(顺位题号是290).给定一个模式和一个字符串str,找到str是否完全匹配该模 ...

  7. LeetCode OJ: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 II 词语模式之二

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

  9. [LeetCode] Word Pattern 词语模式

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

随机推荐

  1. selinux理解1-selinux介绍

    安全增强式Linux(SELinux, Security-Enhanced Linux)是一种强制访问控制(mandatory access control)的实现.它的作法是以最小权限原则(prin ...

  2. 常用 VS 快捷键积累

    1. 代码块大纲显示与隐藏 Ctrl+M,L                   折叠或展开所有代码块 Ctrl+M,M                   折叠或展开当前所在的代码块 Ctrl+M, ...

  3. C# 解析带前缀的Xml节点内容

    一般的xml文件相信大家都会解析了,但是遇到有命名空间的带前缀的xml,对于新手可能会有点问题.我这里在论坛解答的时候就遇到过一题,见怎么获取XML节点里面的内容,在线求教.这里给大家演示一下. 他的 ...

  4. java 子类的实例化和代码块初始化过程

    1,子类的实例化 1,子父类中的构造函数的特点. 在子类构造对象时,发现,访问子类构造函数时,父类也运行了. 为什么呢? 原因是:在子类的构造函数中第一行有一个默认的隐式语句. super(); 子类 ...

  5. 创建一个cocos2d-x工程添加一个自定义Scene并显示

    #include "cocos2d.h" class RunScene :public cocos2d::CCLayer { public: virtual bool init() ...

  6. tar解压去除文件夹

    tar zxvf test.tar.gz  --strip-components 1 解压到当前目录,并去除一级目录

  7. ubuntu14.04配置lnmp

    看到了一片讲解ubuntu下安装lnmp的文章,跟着一步步的来,竟然很顺利的成功了,将文章复制如下,原著勿怪 一.操作步骤 1.安装Nginx sudo apt-get install update ...

  8. 机器学习实战——k-近邻算法

    本章内容 ================================ (一)什么是k-近邻分类算法 (二)怎样从文件中解析和导入数据 (三)使用Matplotlib创建扩散图 (四)对数据进行归 ...

  9. .net entity framework 泛型 更新与增加记录

    static public bool SaveOrUpdate<T>(T entity) where T: class { bool result = false; using (wech ...

  10. Bootstrap简易使用指南

    1.框架 1.1全局样式 使用HTML5的doctype,scaffolding.less中定义全局样式,从2开始使用normalize.css,并使用reset.less进行简化 1.2默认栅格系统 ...