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 ...
随机推荐
- UESTC 33 Area --凸包面积
题意: 求一条直线分凸包两边的面积. 解法: 因为题意会说一定穿过,那么不会有直线与某条边重合的情况.我们只要找到一个直线分成的凸包即可,另一个的面积等于总面积减去那个的面积. 怎么得到分成的一个凸包 ...
- GNU make简要介绍①指定变量、自动推导规则、清除工作目录过程文件
Makefile简介 在执行make之前需要一个命名为Makefile的特殊文件来告诉make需要做些什么. 当使用 make 工具进行编译时,工程中以下几种文件在执行 make 时将会被编译 (重新 ...
- linux(TINY6410)下移植boa服务器
今天在tiny6410上移植了boa服务器,中间遇到了一些小问题.当时也及时解决了. 第一步:boa的编译 在www.boa.org下载Boa的压缩包 我这里用的是 boa-0.94.13.tar. ...
- 一个App需要的东西
1.短信申请平台 (发送验证码需要的短信) http://www.yuntongxun.com/api/sms?nl=sy_cp 容联云通讯
- git生成ssh key 避免每次push都要输入账号密码
第一步:生成public/private rsa key pair在命令行中输入ssh-keygen -t rsa -C "your_email@example.com" 默认在这 ...
- Apache mod_rewrite规则重写的标志一览
1) R[=code](force redirect) 强制外部重定向 强制在替代字符串加上http://thishost[:thisport]/前缀重定向到外部的URL.如果code不指定,将用缺省 ...
- win10显示此电脑
http://jingyan.baidu.com/article/3aed632e00dfe17011809169.html
- iis 发布MVC HTTP错误 403.14
1. 在web.config增加<modules runAllManagedModulesForAllRequests="true" /> 如:问题依然不能解决.报错貌 ...
- Razor 模板自己渲染出结果 string
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNet ...
- 招聘 微软全球技术支持中心 sql server组
微软亚太区全球技术支持中心(APGC CSS)是微软为个人用户.开发者.IT 专业人员到合作伙伴和企业级合作伙伴提供全方位.多元化的服务和技术支持的部门.一个优秀的SQL Server技术支持工程师应 ...