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 ...
随机推荐
- 转: rapidJSON与jsoncpp语法说明
转: http://www.voidcn.com/blog/hudejun007/article/p-1811986.html
- HTML 学习笔记 CSS样式(链接)
我们能够以不同的方法为链接设置样式. 设置链接的样式 能够设置链接样式的 CSS 属性有很多种(例如 color, font-family, background 等等).链接的特殊性在于能够根据它们 ...
- Html5 Egret游戏开发 成语大挑战(六)游戏界面构建和设计
本篇将主要讲解游戏界面的构建和设计,会应用到egret.eui的自定义组件,可以很直观的构建一个游戏整体,这里我们仍然只需要使用EgretWing就可以达到目的,本篇可能是篇幅最少的一个,但是涉及自定 ...
- 安装 SQL SERVER 2008 必须使用 "角色管理工具" 错误 的 解决方案 (转)
刚在服务器(Win2008)上安装SqlServer2008的时候出现了这么一个报错——必须使用“角色管理工具”安装或配置Microsoft .NET Framework 3.5 SP1.一开始以为是 ...
- indows 8上强制Visual Studio以管理员身份运行
http://diaosbook.com/Post/2013/2/28/force-visual-studio-always-run-as-admin-on-windows-8 Windows 8的一 ...
- Linux shell实战(ipcs工具)
#!/bin/bash -o $# -gt ] then echo "参数个数不正确!" exit - fi WHOAIM=`whoami` function release { ...
- javascript:查找“跳号”号码
业务背景:航空货运系统中,“货运代理商”会定期从“航空公司”领取一定数量的纸质运单(每张纸上有一个单号),这些单号都是连续的(即:每次可以理解为领取一个“号段”),而且每张单子都要向航空公司交纳一定的 ...
- list使用例子(转)
例子: 在vs2010中创建一个winform的解决方案,然后定义一个类Person,Person.cs 的代码如下: using System;using System.Collections.Ge ...
- sql 2012 提示列名无效 但可以执行问题
笔者目前使用Ctrl+Shift+R可以解决这个问题,因为智能感知的问题,需要重新整理一下intellisense.有其他方法,请园友共享一下,谢谢. VS2012及13都有用到智能感知,而在sql里 ...
- HoloLens开发手记 - Unity development overview 使用Unity开发概述
Unity Technical Preview for HoloLens最新发行版为:Beta 24,发布于 09/07/2016 开始使用Unity开发HoloLens应用之前,确保你已经安装好了必 ...