注意:

// 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. 使用Assetbundle时可能遇到的坑

    原地址:http://www.cnblogs.com/realtimepixels/p/3652128.html 一 24 十一郎未分类 No Comments 转自 http://www.unity ...

  2. Loadrunner监控Centos

    一.安装必要包 yum istall gcc gcc-c++ rpcbind -y 二.下载安装必要软件rstatd 下载并安装rstatd,下载地址:http://sourceforge.net/p ...

  3. iOS导航栏-导航栏透明

    设置一张透明图片:nav_bargound.png  //导航栏背景     [self.navigationController.navigationBar setBackgroundImage:[ ...

  4. Android中如何查看内存(上)

    文章参照自:http://stackoverflow.com/questions/2298208/how-to-discover-memory-usage-of-my-application-in-a ...

  5. DF学Mysql(三)——索引操作

    概要: 数据库对象索引其实与书的目录非常相似,主要是为了提高从表中检索数据的速度. 由于数据存储在数据库表中,所以索引是创建在数据库表对象上的,由表中的一个字段或多个字段生成的键组成,这些键存储在数据 ...

  6. HDU 4027 Can you answer these queries?(线段树的单点更新+区间查询)

    题目链接 题意 : 给你N个数,进行M次操作,0操作是将区间内的每一个数变成自己的平方根(整数),1操作是求区间和. 思路 :单点更新,区间查询,就是要注意在更新的时候要优化,要不然会超时,因为所有的 ...

  7. 使用动态SQL语句实现简单的行列转置(动态产生列)

    原始数据如下图所示:(商品的销售明细)date=业务日期:Item=商品名称:saleqty=销售数量: -- 建立测试数据(表)create table test (Date varchar(10) ...

  8. Linux网络编程8——对TCP与UDP的简易封装

    引言 每次使用socket通信,都会有很对相似的操作.本文,会对TCP与UDP通信做一简单封装,并生成动态库. 代码 my_socket.h #ifndef __MY_SOCKET_H__ #defi ...

  9. [C#] 區分 abstract、virtual、override 和 new

    abstract.virtual.override和new是在類別的繼承關係中常用的四個修飾方法的關鍵字,在此略作總結. 1. 常用的中文名稱: n   abstract => 抽象方法. n  ...

  10. hibernate中openSession()跟getCurrentSession()方法之间的区别

    Hibernate openSession() 和 getCurrentSession的区别 getHiberanteTemplate .getCurrentSession和OpenSession 采 ...