注意:

// 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. 常用的Python字符串常量

    下面是一些常用的Python字符串常量string.digits:包含0-9的字符串string.letters:包含所有大小写字母的字符串 string.lowercase:所有小写字母string ...

  2. jquery的show/hide性能测试

    这篇文章是jQuery各种 show/hide方式的性能测试.作者之所以测试这个源于Robert Duffy在SanFrancisco举行的jQuery大会上的一句话:“.hide()和.show() ...

  3. GIS数据格式topojson

    Topojson源自于GeoJson,是D3中描述地理数据的格式,D3的作者觉得GeoJson太繁琐.同样的数据,TopoJson是GeoJson的1/5. 这里有一个转换TopoJson,GeoJs ...

  4. MariaDB Galera Cluster 部署(如何快速部署 MariaDB 集群)

    MariaDB Galera Cluster 部署(如何快速部署 MariaDB 集群)  OneAPM蓝海讯通7月3日 发布 推荐 4 推荐 收藏 14 收藏,1.1k 浏览 MariaDB 作为 ...

  5. MariaDB Galera Cluster 部署

    原文  http://code.oneapm.com/database/2015/07/02/mariadb-galera-cluster/MariaDB作为Mysql的一个分支,在开源项目中已经广泛 ...

  6. Sql调用WebService

    DECLARE @scid int,@rt int ) --创建MSSOAP.SoapClient组件(如果安装的是SoapToolkit30,应该是MSSOAP.SoapClient30,否则是MS ...

  7. HDU 2843 I Will Win(概率题?,怨念颇深,简单)

    题目 真不想说什么,,,这神题真讨厌,,,多校的.. //又是一道神题... #include<stdio.h> #include<string.h> //最大公约数 int ...

  8. iOS终端查看.a文件是否能在模拟器上运行

    复制文件路径进去: 红色框框里面没有x86所以模拟器运行会报错

  9. 深入浅出ES6(十五):子类 Subclassing

    作者 Jason Orendorff  github主页  https://github.com/jorendorff 在之前的文章<深入浅出ES6(十三):类 Class>中,我们一起深 ...

  10. Linux网络编程10——使用UDP实现五子棋对战

    思路 1. 通信 为了同步双方的棋盘,每当一方在棋盘上落子之后,都需要发送给对方一个msg消息,让对方知道落子位置.msg结构体如下: /* 用于发给对方的信息 */ typedef struct t ...