word-pattern(mock)
注意:
// 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)的更多相关文章
- [LeetCode] Word Pattern II 词语模式之二
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
- [LeetCode] Word Pattern
Word Pattern Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy Given a pattern and a st ...
- Word Pattern | & II
Word Pattern | Given a pattern and a string str, find if str follows the same pattern. Examples: pat ...
- 291. Word Pattern II
题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- Word Pattern
package cn.edu.xidian.sselab.hashtable; import java.util.HashMap;import java.util.Map; /** * * @au ...
- Word Pattern II 解答
Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- 【leetcode】290. Word Pattern
problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...
- 290. Word Pattern 单词匹配模式
[抄题]: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
随机推荐
- display:inline-block引发的间隙问题解决办法
在网页布局中我们经常会用到display:inline-block;好处是:能够将块状元素按照内联元素的方式布局,同时能设置宽高.个人感觉很好用,可是用多了慢慢的问题就来了? 1.display:in ...
- sublime 配置C++
1. 安装C语言编译器MinGW,并把MinGW安装目录下的bin目录添加到环境变量PATH里.详细方法参照MinGW安装和使用 2. 在SublimeText安装目录下的Data\Packages\ ...
- Unity3D研究院之静态自动检查代码缺陷与隐患
原地址:原地址:http://www.xuanyusong.com/archives/2828 代码缺陷和代码错误的最大区别是,代码缺陷不影响游戏编译,而代码错误编译都不通过.但是代码缺陷会影响游戏发 ...
- 有了 Docker,用 JavaScript 框架开发的 Web 站点也能很好地支持网络爬虫的内容抓取
点这里 阅读目录 用 AngularJS(以及其它 JavaScript 框架)开发的 Web 站点不支持爬虫的抓取 解决方案 为什么公开我们的解决方案 实现 AngularJS 服务 结论 Pr ...
- iOS多线程的初步研究(四)-- NSTimer
理解run loop后,才能彻底理解NSTimer的实现原理,也就是说NSTimer实际上依赖run loop实现的. 先看看NSTimer的两个常用方法: + (NSTimer *)timerWit ...
- Codeigniter 利用加密Key(密钥)的对象注入漏洞
http://drops.wooyun.org/papers/1449 原文链接:http://www.mehmetince.net/codeigniter-object-injection-vuln ...
- 从.NET 1.1 升级到.NET 4.0 遇到 线程间操作无效: 从不是创建控件 [XX] 的线程访问它.
有两种方式解决 1.在窗体构造函数中写Control.CheckForIllegalCrossThreadCalls =false;2.使用Invoke等委托函数 问题原因是 .NET2.0 以后拒绝 ...
- CF 197 DIV2 Xenia and Bit Operations 线段树
线段树!!1A 代码如下: #include<iostream> #include<cstdio> #define lson i<<1 #define rson i ...
- copy
拷贝文件,不覆盖重复文件 yes no|cp -i a b
- C#计算时间差值
/// <summary> /// 计算时间差值 /// </summary> /// <param name="DateTime1">< ...