leetcode290
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的更多相关文章
- [LeetCode290]Word Pattern
题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...
- [Swift]LeetCode290. 单词模式 | Word Pattern
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
随机推荐
- centOS安装ab测试工具
yum install apr-util cd /opt mkdir abtmp cd abtmp yum install yum-utils.noarch yumdownloader httpd-t ...
- UVA-1605 Building for UN (构造)
题目大意:n个国家的人要在一栋大厦里办公,要求任意两个国家的办公室要相邻(同层同边或邻层同面),设计一个满足要求的方案. 题目分析:题目限制较少,任意构造出一个解即可. 代码如下: # include ...
- BZOJ2986 Non-Squarefree Numbers
神马的容斥原理实在是太神啦! 就是先二分一个数mid,看看有几个满足要求的数比他小. 查看的方法就是容斥原理... res = ((2 ^ 2)倍数个数 - ((2 ^ 2) * (3 ^ 2)倍数个 ...
- hdu2883
题解: 网络流 用一个离散化 代码: #include<cstdio> #include<cstring> #include<algorithm> using na ...
- 一次SQLServer索引损坏问题的排查与修复
线上库执行一项数据变更操作时,一直提示"出现错误 8646.请记录该错误和时间,并与您的系统管理员联系." 通过代码排查,最终确定是在执行某存储过程时触发了如下错误,并指明了位置是 ...
- js设计模式中发布与订阅实现观察者模式例子
<script> var pubsub = {}; (function(q) { var topics = {}; subuid = -1; q.publish = function(to ...
- 如何修改windows系统的host文件
方法/步骤 首先我们打开我的计算机 然后我们进入C盘的C:\Windows\System32\drivers\etc这个目录下面找到host这个文件 双点击打开,选择计算本打开,这时可 ...
- 量化投资策略:常见的几种Python回测框架(库)
量化投资策略:常见的几种Python回测框架(库) 原文地址:http://blog.csdn.net/lawme/article/details/51454237 本文章为转载文章.这段时间在研究量 ...
- 用pil产生验证码出现:ImportError: The _imagingft C module is not installed
这个是由于PIL没有编译freetype导致的查看 lib/python2.7/site-packages/PIL/看看 _imagingft.so 是否存在 # 需要先安装jpeg库wget htt ...
- BZOJ5312: 冒险【线段树】【位运算】
Description Kaiser终于成为冒险协会的一员,这次冒险协会派他去冒险,他来到一处古墓,却被大门上的守护神挡住了去路,守护神给出了一个问题, 只有答对了问题才能进入,守护神给出了一个自然数 ...