(String). Word Pattern
Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
Examples:
- pattern =
"abba", str ="dog cat cat dog"should return true. - pattern =
"abba", str ="dog cat cat fish"should return false. - pattern =
"aaaa", str ="dog cat cat dog"should return false. - pattern =
"abba", str ="dog dog dog dog"should return falsepublic class Solution { //if不用hashmap,更好的方法是设置头尾“指针”,保证一个指向当前的值
public boolean wordPattern(String pattern, String str) {
String[] strs = str.split(" ");
if (pattern.length() != strs.length)
return false;
Map<Character, String> map = new HashMap<Character, String>();
for (int i = 0; i < pattern.length(); i++) {
if (map.containsKey(pattern.charAt(i))
&& !(map.get(pattern.charAt(i))).equals(strs[i]))
return false;
if (!map.containsKey(pattern.charAt(i))
&& map.containsValue(strs[i]))
return false;
if (!map.containsKey(pattern.charAt(i))
&& !map.containsValue(strs[i]))
map.put(pattern.charAt(i), strs[i]);
}
return true;
}
}
(String). Word Pattern的更多相关文章
- [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 ...
- 290. Word Pattern
题目: 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 ...
随机推荐
- Linux软件的安装方法!!!
1.yum/rpm(*.rpm) 包管理器:直接yum/rpm安装. 优点:是全自动化安装,不需要为依赖问题发愁,缺点是自主性太差,软件的功能.存放位置固定,不易变更. 2.源码包(*.tar.gz) ...
- CLR via C# 3rd - 06 - Type and Member Basics
1. Different Kinds of Type Members A type can define zero or more of the following kinds of ...
- C++ 虚函数,纯虚函数的一些问题
#include <iostream> using namespace std; #define cendl cout << endl; class AA{//这是一个纯虚函数 ...
- dede currentstyle属性完美解决方案
问题一.dede让channelartlist标签支持currentstyle属性 完美解决 打开include\taglib\channelartlist.lib.php找到$pv->Fiel ...
- hdu 1010 深搜+剪枝
深度搜索 剪枝 还不是很理解 贴上众神代码 //http://blog.csdn.net/vsooda/article/details/7884772#include<iostream> ...
- 井间数据polarization analysis 相关概念
1. 垂直分量上记录到的数据,无法记录SH波?这个有待考证,先记录于此~ 两点需要注意:1.层状介质中,P波和深度方向(Z轴)组成入射面;2.SH的定义为垂直于入射面的S波分量. 2.VSP的观测方式 ...
- 用Join子句进行分组联接
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- c#事件机制
namespace test { class Publisher//出版社 { public delegate void PubComputer(string magazineName);//声明事件 ...
- linux 几个控制流语句的格式例子(if语句)
linux 几个控制流语句的格式例子:if 语句例子:#!/bin/sh a=10b=20 if [ $a == $b ]then echo "a is equal to b"el ...
- Bulkcopy对应的实现是Oracle的SQL*LOADER,期间造成Index Unusable,并且last_ddl_time上是不体现的
部分项目反馈系统整体突然变慢,经查询发现一个系统核心的大数据表的索引状态全部是Unusable. 导致索引失效的直接原因:当某些操作导致数据的rowid改变,索引就会完全失效. 那什么时候会导致row ...