public class Solution {
public bool WordPattern(string pattern, string str) {
var list = str.Split(' ').ToList(); int type1 = ;
int type2 = ; StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder(); Dictionary<string, int> dic1 = new Dictionary<string, int>();
Dictionary<char, int> dic2 = new Dictionary<char, int>(); foreach (var word in list)
{
if (!dic1.ContainsKey(word))
{
dic1.Add(word, type1);
sb1.Append(type1);
type1++;
}
else
{
sb1.Append(dic1[word]);
}
} foreach (var c in pattern)
{
if (!dic2.ContainsKey(c))
{
dic2.Add(c, type2);
sb2.Append(type2);
type2++;
}
else
{
sb2.Append(dic2[c]);
}
} if (sb1.ToString() != sb2.ToString())
{
return false;
}
else
{
return true;
}
}
}

https://leetcode.com/problems/word-pattern/#/description

leetcode290的更多相关文章

  1. [LeetCode290]Word Pattern

    题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...

  2. [Swift]LeetCode290. 单词模式 | Word Pattern

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

随机推荐

  1. 在Linux和Windows系统上安装Nginx服务器的教程

    在Linux和Windows系统上安装Nginx服务器的教程  1.在CentOS系统上安装Nginx 在 CentOS6 版本的 EPEL 源中,已经加入了 nginx 的 rpm 包,不过此 RP ...

  2. hdu5015矩阵快速幂

    参考博客:http://blog.csdn.net/rowanhaoa/article/details/39343769 反正递推关系式推了一个多小时没搞出来...太弱了 真是愧对数学系这一专业了.. ...

  3. 本地RUN Page时报无法显示该网页

    经检查,是我本地安装了浏览器广告屏蔽插件引起的,关闭该插件即可.

  4. Java中如何读写cookie (二)

    Java中删除cookie Cookie[]   cookies=request.getCookies();       //cookies不为空,则清除       if(cookies!=null ...

  5. reg文件中文乱码

    用reg文件方式修改注册表很方便,但是如果字段中有中文的话,执行reg文件后,注册表中的中文出现乱码. 解决办法:将文件保存文Unicode编码方式即可.(保存文UTF8编码也是不行的) 附一个添加\ ...

  6. Total Defense Anti-Virus – 免费6个月

    Total Defense Anti-Virus 具有病毒和间谍软件保护,免费的专家远程安装和设置,快速扫描引擎不会拖慢电脑速度,基于云保护.高级Rootkit保护,自动扫描驱动器和内存等等.官方网站 ...

  7. antd中form自定义rules

    1.使用getFieldDecorator <FormItem label="手机号" > {getFieldDecorator('phone', { initialV ...

  8. 解决maven install报错信息(Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile )

    Maven install失败 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (d ...

  9. Linux内核gpiolib注册建立过程

    1.相关的数据结构 struct s3c_gpio_chip { // 这个结构体是三星在移植gpiolib时封装的一个结构体 用来描述一组gpio端口信息 struct gpio_chip chip ...

  10. udev学习笔记汇总

    1.什么是udev udev--就是动态设备管理 udev 能够处理设备事件.管理设备文件的权限.在/dev目录中创建额外的符号链接.重命名网络接口,等等. 内核通常仅根据设备被发现的先后顺序给设备文 ...