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 ...
随机推荐
- instanceof
java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例. instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例. result ...
- Collider Collision 区别
Collision 中带有碰撞的信息,例如:速度和撞击到的点 示例 void OnCollisionEnter2D(Collision2D coll) { foreach(ContactPoint c ...
- 给VIM安装插件。让ubuntu的vim强大起来
简易安装方法: 打开终端,执行下面的命令就自动安装好了: wget https://raw.github.com/ma6174/vim/master/setup.sh -O ma6174_vim_se ...
- 十步完全理解SQL
转载于:http://blog.jobbole.com/55086/ 很多程序员视 SQL 为洪水猛兽.SQL 是一种为数不多的声明性语言,它的运行方式完全不同于我们所熟知的命令行语言.面向对象的程序 ...
- 学习cocos 空程序
今天开始学习cocos代码,首先研究源码中的空程序. 在这个程序中,在main函数中,创建了一个Application: int APIENTRY _tWinMain(HINSTANCE hInsta ...
- Anyconnect的VPN环境部署(2)-在Linux客户机上连接Anyconnect
由于之前已经在机房IDC安装了Anyconnect的VPN服务环境(参考:Anyconnect的VPN环境部署(1)-OpenConnect server(ocserv)服务安装)今天介绍下在linu ...
- javascript数组对象排序
javascript数组对象排序 JavaScript数组内置排序函数 javascript内置的sort函数是多种排序算法的集合 JavaScript实现多维数组.对象数组排序,其实用的就是原生的s ...
- C语言--scanf
关于C语言的scanf,首先看个例子 int get_int(void){ int input; char ch; ){ printf("is not an integer,please e ...
- Android fragment之间消息传递
1. 在Fragment里定义一个内部接口, 在Fragment初始化方法里, 把父Activity转换成这个接口, 赋值给成员变量 public class DummyFragment extend ...
- ROS(Robot Operating System)常用环境变量介绍
本文简单介绍ROS系统中常用的环境变量用途及设置方式.ROS系统环境中除了必须配置的环境变量以外,其他的也是十分有用,通过修改变量路径,可以设置ROS系统中log文件存放路径,单元测试结果存放路径等. ...