zoukankan      html  css  js  c++  java
  • LeetCode Word Pattern II

    原题链接在这里:https://leetcode.com/problems/word-pattern-ii/

    题目:

    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.

    题解:

    类似Word Pattern. 但这里没有明确分割words的空格.

    采用backtracking, 用pattern内的一个char来试着match不同长度的str substring, 能同时试到pattern 和 str的结尾, 就返回true.

    e.g. pattern = "abab", str = "redblueredblue", 先用"a" match "r", "b" match "e", 又遇到"a", 但str里确实"d", 不match.

    就backtracking下然后用"b" match "ed", 以此类推.

    用HashMap<Character, String> 来保存已经走过的pattern char 和 str substring对应关系.

    同时maintain一个set用来避免相同的string 来match 不同的char. e.g. "a" match "cde", "b"也match "cde".

    Time Complexity: exponential. Space: O(hm.size() + hs.size()).

    AC Java: 

     1 public class Solution {
     2     public boolean wordPatternMatch(String pattern, String str) {
     3         HashMap<Character, String> hm = new HashMap<Character, String>();
     4         HashSet<String> hs = new HashSet<String>();
     5         return isMatch(pattern, 0, str, 0, hm, hs);
     6     }
     7     
     8     private boolean isMatch(String pattern, int i, String str, int j, HashMap<Character, String> hm, HashSet<String> hs){
     9         //stop condition
    10         if(i == pattern.length() && j == str.length()){
    11             return true;
    12         }
    13         if(i == pattern.length() || j == str.length()){
    14             return false;
    15         }
    16         
    17         char c = pattern.charAt(i);
    18         
    19         //If current pattern character already exists in map
    20         if(hm.containsKey(c)){
    21             String cString = hm.get(c);
    22             
    23             //If corresponding string does not match str.substring(j, j+matchedString.length()).
    24             if(!str.startsWith(cString, j)){
    25                 return false;
    26             }
    27             return isMatch(pattern, i+1, str, j+cString.length(), hm, hs);
    28         }else{
    29             //current pattern character doesn't exist in the map
    30             for(int k = j; k<str.length(); k++){
    31                 String s = str.substring(j, k+1);
    32                 
    33                 //different strings can't be matched to same char
    34                 if(hs.contains(s)){
    35                     continue;
    36                 }
    37                 
    38                 hm.put(c, s);
    39                 hs.add(s);
    40                 
    41                 //if all reset string could be matched
    42                 if(isMatch(pattern, i+1, str, k+1, hm, hs)){
    43                     return true;
    44                 }
    45                 
    46                 hm.remove(c);
    47                 hs.remove(s);
    48             }
    49             
    50             //Have tried all different lengths
    51             return false;
    52         }
    53     }
    54 }
  • 相关阅读:
    【python】使用matplotlib绘制多个子图时出现标题、轴标签等文字重叠的解决
    【python】反转字典键值
    关于MacOs python 安装tensorflow1.8版本时出现的问题及解决
    【Python】爬取百度图片进行人脸识别
    【转载】利用python、tensorflow、opencv实现人脸识别
    ubuntu16.04源码方式安装配置nginx
    ubuntu16.04 14.04更换源
    stdClass类
    redis常用配置项
    redis主从模式
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/6351250.html
Copyright © 2011-2022 走看看