leetcode(一)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 false.
本人思路:把字符串转化为字符串数组后,先比较长度;再用pattern里的字符替换掉字符串。全部替换后再重新转化为字符串,并与pattern字符串相比较,得出结论。
代码如下(方法略笨拙,可跳过看第二种,哈哈哈):
public bool WordPattern(string pattern, string str) {
//char[] patternAttr=new char[pattern.Length]
char[] patternAttr=pattern.ToCharArray();
string[] strAttr=str.Split(' ');
if(patternAttr.Length!=strAttr.Length)
{
return false;
}
else
{
for(int i=;i<strAttr.Length;i++)
{
if (patternAttr[i] != ' ')
{
for(int j=i+;j<strAttr.Length;j++)
{ if(strAttr[j]==strAttr[i])
{
strAttr[j]=patternAttr[i].ToString()+"!";
patternAttr[j] = ' ';
}
}
for (int k = i + ; k < strAttr.Length;k++ )
{
if(patternAttr[k]==patternAttr[i])
{
patternAttr[i] = ' ';
}
}
strAttr[i] = patternAttr[i].ToString()+"!";
patternAttr[i] = ' ';
}
}
str=String.Join("",strAttr);
str=str.Replace("!","");
if(str==pattern)return true;
else return false;
}
}
第二种解题方法:用字典
public class Solution {
public bool WordPattern(string pattern, string str) {
string[] values = str.Split(' ');
if(pattern.Length!=values.Length)
{
return false;
}
Dictionary<Char,String> dic=new Dictionary<Char,String>();
for(int i=;i<pattern.Length;i++)
{
if(!dic.ContainsKey(pattern[i]))
{
if (!dic.ContainsValue(values[i]))
{
dic.Add(pattern[i], values[i]);
}
else return false;
}
else
{
if(!dic[pattern[i]].Equals(values[i]))
{
return false;
}
}
}
return true;
}
}
leetcode(一)Word Pattern的更多相关文章
- [LeetCode] 290. Word Pattern 单词模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)
翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...
- [LeetCode] 290. Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] 291. Word Pattern II 词语模式 II
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- leetcode 290. Word Pattern 、lintcode 829. Word Pattern II
290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...
- LeetCode 290. Word Pattern (词语模式)
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- LeetCode 290 Word Pattern
Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- Leetcode 290 Word Pattern STL
Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...
- Java [Leetcode 290]Word Pattern
题目描述: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
随机推荐
- 关于AngularJs中的路由学习总结
AngularJs中的路由,应用比较广泛,主要是允许我们通过不同的url访问不同的内容,可实现多视图的单页web应用.下面看看具体怎么使用. 关于路由 通常我们的URL形式为http://jtjds ...
- Could not load file or assembly 'MySql.Data.CF,
Could not load file or assembly 'MySql.Data.CF, Version=6.4.4.0, Culture=neutral, PublicKeyToken=c56 ...
- C#小游戏(文字对战游戏)
第一代,不是很完善,会在后续增加更多的功能 主: using System; using System.Collections.Generic; using System.Linq; using Sy ...
- 向Tiny6410移植QT4.7.0版本
在移植QT之前我们首先要安装tslib,没有移植的可以看这篇文章移植. http://www.cnblogs.com/ynxf/p/5392476.html step 1: echo yes |../ ...
- request.getRequestDispather().forward()与response.sendRedirect()
request.getRequestDispather().forward(),是服务器端的跳转,地址栏无变化. response.sendRedirect()是客户端的跳转,地址栏发生变化.
- sublime和python--path
配置Sublime Text 2 的Python运行环境 (2013-09-11 11:36:17) 转载▼ 标签: python 分类: 科技相关 Sublime Text 2作为一款轻量级 ...
- 使用SecureCRT连接AWS EC2
AWS提供的XXX.pem文件, 如果使用Ubuntu等linux系统,直接使用ssh命令即可访问AWS上的Linux-EC2实例. $ ssh -i XXX.pem ec2-user@{IP/hos ...
- Linux 网络编程详解八
TCP/IP协议三次握手机制 TCP/IP是全双工通道,两端都可以读写,三次握手机制就是验证TCP/IP是否是全双工通道 1.客户端调用connect()函数,阻塞客户端进程,客户端向服务器发送数据包 ...
- Mybatis.Net 整合 ODP.NET Managed
初步接触MyBatis.Net的朋友,请先移步 MyBatis.Net 学习手记 1. 项目中先添加Oracle.ManagedDataAccess.dll程序集引用 2. MyBatis.Net 中 ...
- opencv6.2-imgproc图像处理模块之图像尺寸上的操作及阈值
接opencv6.1-imgproc图像处理模块之平滑和形态学操作,顺带说一句在opencv中的in-place操作就是比如函数的输入图像和输出图像两个指针是相同的,那么就是in-place操作了.比 ...