注意:

// String要用equals,不然比较结果不对,会出bug
//
使用String.split

// boolean打印用 %b

 // abba 对应 cccc 也不行所以要用set记录

https://leetcode.com/problems/word-pattern/
https://leetcode.com/mockinterview/session/result/xsl1lbt/
package com.company; import java.util.*; class Solution {
public boolean wordPattern(String pattern, String str) {
// String要用equals,不然比较结果不对,会出bug
// abba 对应 cccc 也不行, 所以要用set记录
// 使用String.split String[] strs = str.split(" "); if (pattern.length() != strs.length) {
System.out.println("here1");
return false;
} Map<String, String> mp = new HashMap<>();
Set<String> st = new HashSet<>(); for (int i=0; i<pattern.length(); i++) {
String key = pattern.substring(i, i+1);
if (mp.containsKey(key)) {
// 开始用的 != 是错误的。要用equals
if (!mp.get(key).equals(strs[i])) {
System.out.printf("k: %s, v: %s, str: %s\n", key, mp.get(key), strs[i]);
return false;
}
}
else {
if (st.contains(strs[i])) {
return false;
}
else {
mp.put(key, strs[i]);
st.add(strs[i]);
}
}
}
return true;
}
} public class Main { public static void main(String[] args) {
// write your code here
System.out.println("Hello"); String pattern = "abba";
String str = "dog dog dog doa"; Solution solution = new Solution();
boolean ret = solution.wordPattern(pattern, str);
System.out.printf("Get ret: %b\n", ret); }
}

word-pattern(mock)的更多相关文章

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

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

  2. [LeetCode] Word Pattern 词语模式

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

  3. [LeetCode] Word Pattern

    Word Pattern Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy Given a pattern and a st ...

  4. Word Pattern | & II

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

  5. 291. Word Pattern II

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

  6. leetcode面试准备: Word Pattern

    leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...

  7. Word Pattern

    ​package cn.edu.xidian.sselab.hashtable; import java.util.HashMap;import java.util.Map; /** *  * @au ...

  8. Word Pattern II 解答

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

  9. 【leetcode】290. Word Pattern

    problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...

  10. 290. Word Pattern 单词匹配模式

    [抄题]: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...

随机推荐

  1. geotools解析SLD中的elsefilter为什么里面的filter无效

    原因是在org.geotools.renderer.lite.StreamingRenderer中的process函数: /** * @param rf * @param feature * @par ...

  2. sql server 2008 执行计划

    SSMS允许我们查看一个图形化的执行计划(快捷键Ctrl+L)

  3. 常见的NoSql系统使用场景分析--转载

    •Cassandra •特性:分布式与复制的权衡\根据列和键范围进行查询\BigTable类似的功能:列,列族\写比读快很多 •最佳适用:写操作较多,读比较少的时候.如果你的系统都是基于Java的时候 ...

  4. hdu 2571 命运(递推,请小心)

    题目 //不能广搜,会超内存//可以用dp思想模拟//map 后来保存的是 保存由前面推来的最大的幸运总值的点//下标从1开始,不然倍数会有问题 //AC 代码: AC代码 //不能广搜,会超内存 / ...

  5. 历代诗词咏宁夏注释1----常星景:< 六盘>

    六盘 常星景 关中形势甲天下,四岳分峙西太华.[1] 中有汭泾经纬之,六盘嵚崎历历落.[2] □□□□其流亚,终年峰头雪不消. 弟畜太白兒美高,眼底培缕纷纷何足数,呼吸想通天尺五.[3] 西北堆镇一切 ...

  6. mysql 中的bool值

    boolean在MySQL里的类型为tinyint(1) 很奇怪.

  7. [你必须知道的.NET]第三十二回,,深入.NET 4.0之,Tuple一二

    发布日期:2009.06.01 作者:Anytao © 2009 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. Tuple,是函数式编程的概念之一,早见于Elang.F#等动态 ...

  8. 使用Ninject来解决程序中组件的耦合问题

    1.为什么要用Ninject? Ninject是一个IOC容器用来解决程序中组件的耦合问题,它的目的在于做到最少配置.其他的的IOC工具过于依赖配置文件,需要使用assembly-qualified名 ...

  9. PHP5.4最新特性

     PHP5.4最新特性   官网:ChangeLog-5.php#5.4.0 原文Oracle:LAMP 体系有了新的竞争,但此版本中的特性使 PHP 再次挑战极限. 稍微做了修改.: 概述总结:1. ...

  10. UVA 11076 Add Again 计算对答案的贡献+组合数学

    A pair of numbers has a unique LCM but a single number can be the LCM of more than one possiblepairs ...