Question

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.

Solution

Word Pattern 是用HashMap的containsKey()和containsValue()两个方法实现的。

但是对于Word Pattern II,我们并不知道怎样划分str,所以基本想法是“暴力搜索”即backtracking/DFS。这里DFS的存储对象不是常见的list,而是map.

 public class Solution {
public boolean wordPatternMatch(String pattern, String str) {
return helper(pattern, 0, str, 0, new HashMap<Character, String>());
} private boolean helper(String pattern, int startP, String str, int startS, Map<Character, String> map) {
if (startP == pattern.length() && startS == str.length()) {
return true;
} else if (startP == pattern.length()) { }
if (match.equals(str.substring(startS, endS))) {
return helper(pattern, startP + 1, str, endS, map);
} else {
return false;
}
} else {
// If map does not have existing key
// Traverse, brute force for (int i = startS + 1; i <= str.length(); i++) {
String candidate = str.substring(startS, i);
if (map.containsValue(candidate)) {
continue;
}
map.put(key, candidate);
if (helper(pattern, startP + 1, str, i, map)) {
return true;
}
map.remove(key);
}
}
return false;
}
}

Word Pattern II 解答的更多相关文章

  1. Word Pattern | & II

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

  2. leetcode 290. Word Pattern 、lintcode 829. Word Pattern II

    290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...

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

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

  4. 291. Word Pattern II

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

  5. Leetcode solution 291: Word Pattern II

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

  6. [LeetCode] 291. Word Pattern II 词语模式 II

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

  7. Leetcode: Word Pattern II

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

  8. Word Break II 解答

    Question Given a string s and a dictionary of words dict, add spaces in s to construct a sentence wh ...

  9. Word Search II 解答

    Question Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...

随机推荐

  1. 【剑指offer】面试题36:数组中的逆序对

    题目: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. 思路: 归并排序的合并过程.主要是考虑合并两个有序序列时,计算逆序 ...

  2. Apache+php配置 Mysql安装出错解决办法

    此文包括的注意内容:软件版本及下载地址Apache2.4的配置和安装php7.0的配置mysql5.5的安装常见问题及解决方法1.软件版本Windows server 2008 r2+ 64位Apac ...

  3. Shiro学习详解

    1.Shiro基本架构 一.什么是Shiro Apache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能: 认证 - 用户身份识别,常被称为用户“登录”: 授权 ...

  4. java_接口的应用

    package com.test; interface USB{ //创建一个USB接口,所有的操作要按照这个标准来工作 void start();//默认为public void stop(); } ...

  5. (转)Building MariaDB on Mac OS X using Homebrew

    https://kb.askmonty.org/en/building-mariadb-on-mac-os-x-using-homebrew/ Work has been done to provid ...

  6. [原创作品]手把手教你怎么写jQuery插件

    这次随笔,向大家介绍如何编写jQuery插件.啰嗦一下,很希望各位IT界的‘攻城狮’们能和大家一起分享,一起成长.点击左边我头像下边的“加入qq群”,一起分享,一起交流,当然,可以一起吹水.哈,不废话 ...

  7. iPhone应用提交流程:如何将App程序发布到App Store?

    对于刚加入iOS应用开发行列的开发者来说,终于经过艰苦的Coding后完成了第一个应用后最重要的历史时刻就是将应用程序提交到iTunes App Store.Xcode 4.2开发工具已经把App提交 ...

  8. 信息熵(Entropy)究竟是用来衡量什么的?

    信息熵(Entropy)究竟是用来衡量什么的? ——与Philip ZHANG商榷 思明 Philip ZHANG先生在反驳彭小明的时候,提出一个观点,他说:“ 就语言文 字来说,总体效率不是用民族主 ...

  9. C++——try、throw、catch实例学习程序

    #include<iostream> #include<stdexcept> //exception/stdexcept/new/type_info头文件里都有定义的标准异常类 ...

  10. [Regular Expressions] Find Repeated Patterns

    Regular Expression Quantifiers allow us to identify a repeating sequence of characters of minimum an ...