博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
290. Word Pattern && 291. Word Pattern II
阅读量:5360 次
发布时间:2019-06-15

本文共 1723 字,大约阅读时间需要 5 分钟。

290. Word Pattern

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-emptyword in str.

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:

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

 
Hide Tags
 
Hide Similar Problems
 
 
 
 
public class Solution {    public boolean wordPattern(String pattern, String str) {        String[] words = str.split(" ");        if (words.length != pattern.length())          return false;        Map
wordMap = new HashMap<>(); Map
charMap = new HashMap<>(); for (Integer i = 0; i < words.length; ++i) if (!Objects.equals(charMap.put(pattern.charAt(i), i), wordMap.put(words[i], i))) return false; return true; }}

 

291. Word Pattern II

Problem Description:

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 substring in str.

Examples:

    1. pattern = "abab", str = "redblueredblue" should return true.
    2. pattern = "aaaa", str = "asdasdasdasd" should return true.
    3. pattern = "aabb", str = "xyzabcxzyabc" should return false. 

Notes:

You may assume both pattern and str contains only lowercase letters.

 

转载于:https://www.cnblogs.com/neweracoding/p/5653141.html

你可能感兴趣的文章
关于python中带下划线的变量和函数 的意义
查看>>
linux清空日志文件内容 (转)
查看>>
Servlet接收JSP参数乱码问题解决办法
查看>>
Ajax : load()
查看>>
MySQL-EXPLAIN执行计划Extra解释
查看>>
Zookeeper概述
查看>>
Linux自己安装redis扩展
查看>>
luoguP3414 SAC#1 - 组合数
查看>>
图片点击轮播(三)-----2017-04-05
查看>>
直播技术细节3
查看>>
《分布式服务架构:原理、设计于实战》总结
查看>>
java中new一个对象和对象=null有什么区别
查看>>
字母和数字键的键码值(keyCode)
查看>>
IE8调用window.open导出EXCEL文件题目
查看>>
Spring mvc初学
查看>>
VTKMY 3.3 VS 2010 Configuration 配置
查看>>
01_1_准备ibatis环境
查看>>
windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决
查看>>
JavaScript中的BOM和DOM
查看>>
360浏览器兼容模式 不能$.post (不是a 连接 onclick的问题!!)
查看>>