Word Pattern II

原创 2015年11月19日 13:54:33

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-emptysubstring 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://leetcode.com/discuss/63252/share-my-java-backtracking-solution

public class Solution {

  public boolean wordPatternMatch(String pattern, String str) {
    Map<Character, String> map = new HashMap<>();
    Set<String> set = new HashSet<>();

    return isMatch(str, 0, pattern, 0, map, set);
  }

  boolean isMatch(String str, int i, String pat, int j, Map<Character, String> map, Set<String> set) {
    // base case
    if (i == str.length() && j == pat.length()) return true;
    if (i == str.length() || j == pat.length()) return false;

    // get current pattern character
    char c = pat.charAt(j);

    // if the pattern character exists
    if (map.containsKey(c)) {
      String s = map.get(c);

      // then check if we can use it to match str[i...i+s.length()]
      if (!str.startsWith(s, i)) {
        return false;
      }

      // if it can match, great, continue to match the rest
      return isMatch(str, i + s.length(), pat, j + 1, map, set);
    }

    // pattern character does not exist in the map
    for (int k = i; k < str.length(); k++) {
      String p = str.substring(i, k + 1);

      if (set.contains(p)) {
        continue;
      }

      // create or update it
      map.put(c, p);
      set.add(p);

      // continue to match the rest
      if (isMatch(str, k + 1, pat, j + 1, map, set)) {
        return true;
      }

      // backtracking
      map.remove(c);
      set.remove(p);
    }

    // we've tried our best but still no luck
    return false;
  }

}


相关文章推荐

leetcode 290: Word Pattern

Word Pattern Total Accepted: 1733 Total Submissions: 6204 Difficulty: Easy Given a...
  • xudli
  • xudli
  • 2015年10月06日 16:10
  • 4748

[Leetcode]Word Pattern

Given a pattern and a string str, find if str follows the same pattern. Here follow means a full m...
  • Javasus
  • Javasus
  • 2015年10月19日 16:32
  • 629

【leetcode】290. Word Pattern

解题思路:使用两个map结构,注意要防止abba dog dog dog dog 的情形,所以需要两个Map class Solution { public: bool wordPatt...

[LeetCode 290] Word Pattern

Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "a...
  • sbitswc
  • sbitswc
  • 2015年10月09日 11:44
  • 3533

[LeetCode291]Word Pattern II

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

[leetcode] 291. Word Pattern II 解题报告

题目链接: https://leetcode.com/problems/word-pattern-ii/ Given a pattern and a string str, find i...

[Leetcode] 291. Word Pattern II 解题报告

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

LeetCode 291. Word Pattern II(单词模式II)

原题网址:https://leetcode.com/problems/word-pattern-ii/ Given a pattern and a string str, find if str...
  • jmspan
  • jmspan
  • 2016年04月16日 04:00
  • 403

Pattern Oriented Software Architecture II

  • 2007年06月02日 03:03
  • 3.88MB
  • 下载

Word Pattern

  • 2016年05月15日 23:48
  • 981B
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Word Pattern II
举报原因:
原因补充:

(最多只允许输入30个字)