注意:

// 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. 关于mysql 连接数

    mysql基本维护,就必须要知道连接数 进入mysql ,show status    Threads_connected  当前的连接数    Connections  试图连接到(不管是否成功)M ...

  2. Rust: move和borrow

    感觉Rust官方的学习文档里关于ownship,borrow和lifetime介绍的太简略了,无法真正理解这些语法设计的原因以及如何使用(特别是lifetime).所以找了一些相关的blog来看,总结 ...

  3. 使用行为树(Behavior Tree)实现网游奖励掉落系统

    原地址:http://blog.csdn.net/akara/article/details/6165421 [原创]使用行为树(Behavior Tree)实现网游奖励掉落系统by AKara 20 ...

  4. Sqli-labs less 59

    Less-59 与less58一致,直接给出一个示例payload: http://127.0.0.1/sqli-labs/Less-59/?id=-1 union select extractval ...

  5. Sqli-labs less 64

    Less-64 此处的sql语句为 $sql="SELECT * FROM security.users WHERE id=(($id)) LIMIT 0,1"; 示例payloa ...

  6. 最长连续回文串(最优线性时间O(n))

    转自:http://blog.csdn.net/hopeztm/article/details/7932245 Given a string S, find the longest palindrom ...

  7. POJ 2142 The Balance (解不定方程,找最小值)

    这题实际解不定方程:ax+by=c只不过题目要求我们解出的x和y 满足|x|+|y|最小,当|x|+|y|相同时,满足|ax|+|by|最小.首先用扩展欧几里德,很容易得出x和y的解.一开始不妨令a& ...

  8. css display visibility

    当visibility被设置为"hidden"的时候,元素虽然被隐藏了,但它仍然占据它原来所在的位置.注意,当元素被隐藏之后,就不能再接收到其它事件了. display属性就有一点 ...

  9. ElasticSearch使用IK中文分词---安装步骤记录

    提示1:必须保证之前的ES中不存在index, 否则ES集群无法启动, 会提示red! 提示2:下载的IK如果太新,会报错 TokenStream被重载Caused by: java.lang.Ver ...

  10. 由枚举模块到ring0内存结构 (分析NtQueryVirtualMemory)

    是由获得进程模块而引发的一系列的问题,首先,在ring3层下枚举进程模块有ToolHelp,Psapi,还可以通过在ntdll中获得ZwQuerySystemInformation的函数地址来枚举,其 ...