Word Pattern |

Given a pattern and a string str, find if str follows the same pattern.

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:

  1. patterncontains only lowercase alphabetical letters, and str contains words separated by a single space. Each word in str contains only lowercase alphabetical letters.
  2. Both pattern and str do not have leading or trailing spaces.
  3. Each letter in pattern must map to a word with length that is at least 1.

solution:

Split the string, and add the pair to hashmap, if the existing pattern in the hashmap doesn't match the current one, return false.

     public boolean wordPattern(String pattern, String str) {
String[] strs = str.split(" ");
if (pattern.length() != strs.length) return false;
Map<Character, String> map = new HashMap<Character, String>();
for (int i = ; i < pattern.length(); i++) {
if (!map.containsKey(pattern.charAt(i))) {
if (map.containsValue(strs[i])) return false;
map.put(pattern.charAt(i), strs[i]);
} else {
if (!strs[i].equals(map.get(pattern.charAt(i)))) return false;
}
}
return true;
}

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.

分析:

As we don't know the breaking point in str, so we have to try one by one. Onc both the pattern string and str string are empty at the same time, it means the pattern we used is correct.

 public boolean wordPatternMatch(String pattern, String str) {
return getMapping(pattern, str, new HashMap<Character, String>());
} public boolean getMapping(String pattern, String str, HashMap<Character, String> mapping) {
if (pattern.isEmpty() && str.isEmpty()) {
return true;
} else if (pattern.isEmpty() || str.isEmpty()) {
return false;
} if (mapping.containsKey(pattern.charAt())) {
String map = mapping.get(pattern.charAt());
if (str.length() >= map.length() && str.substring(, map.length()).equals(map)) {
if (getMapping(pattern.substring(), str.substring(map.length()), mapping)) {
return true;
}
}
} else {
for (int i = ; i <= str.length(); i++) { // try each pattern
String p = str.substring(, i);
if (mapping.containsValue(p)) continue; // the upper if condition is its opposite
mapping.put(pattern.charAt(), p);
if (getMapping(pattern.substring(), str.substring(i), mapping)) {
return true;
}
mapping.remove(pattern.charAt());
}
}
return false;
}

Word Pattern | & II的更多相关文章

  1. Word Pattern II 解答

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

  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. [Swift]LeetCode291. 单词模式 II $ Word Pattern II

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

  9. [LeetCode] Word Pattern 词语模式

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

随机推荐

  1. Java-异常Throwable,Exception,Error

      异常指不期而至的各种状况,如:文件找不到.网络连接失败.非法参数等. 异常是一个事件,它发生在程序运行期间,干扰了正常的指令流程. Java通过API中Throwable类的众多子类描述各种不同的 ...

  2. Java设计模式-观察者模式(Observer)

    包括这个模式在内的接下来的四个模式,都是类和类之间的关系,不涉及到继承,学的时候应该 记得归纳,记得本文最开始的那个图.观察者模式很好理解,类似于邮件订阅和RSS订阅,当我们浏览一些博客或wiki时, ...

  3. Java设计模式-代理模式(Proxy)

    其实每个模式名称就表明了该模式的作用,代理模式就是多一个代理类出来,替原对象进行一些操作,比如我们在租房子的时候回去找中介,为什么呢?因为你对该地区房屋的信息掌握的不够全面,希望找一个更熟悉的人去帮你 ...

  4. BZOJ-1202 狡猾的商人 并查集+前缀和

    我记得这个题,上次之前做的时候没改完,撂下了,今天突然想改发现,woc肿么A 了= =看来是我记错了.. 1202: [HNOI2005]狡猾的商人 Time Limit: 10 Sec Memory ...

  5. AU3学习资源

    AU3中文站:http://www.autoitx.com/

  6. easyVS

    easyVS 详细说明点这里

  7. bzoj2096 pilots

    依旧是维护两个单调队列,只是队首检查的方式略有变动 /*by SilverN*/ #include<iostream> #include<algorithm> #include ...

  8. excel公式处理成绩表

    一共有2个需求: 1.平均分:所有每个人的成绩/29;及格率:60分的/29;优秀率:80分/29 2.对总分进行排序,并在另一列中生成排名 平均分:=(c3+c4+.......c31)/29 及格 ...

  9. LUA的编译、环境等

    Lua的环境.编译等 Lua命令行 lua命令行选项: -i:进入交互式 -e:执行lua代码 -l:加载库文件 例如使用下面的命令启动lua解释器,可以重新定义lua提示符. lua -i -e & ...

  10. html5浮动、等高、弹性盒模型

    1px dashed虚线 box-sizing拯救了布局 1.inherit  继承父级 2.content-box(默认)-----这个盒子的边框.内边距 这2个值是不包括在width和height ...