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. 更新.xsd后,rdlc 数据源更新不了

  2. js设计模式整理

    单例模式 恶汉式单例 实例化时 懒汉式单例 调用时构造函数模式 1.实现一 function Car(model, year, miles) { this.model = model; this.ye ...

  3. js网页 唤醒支付宝

    过渡页: <script> window.location.href = 'alipays://platformapi/startApp?appId=10000011&url=al ...

  4. ubuntu16.04安装Nvidia显卡驱动、CUDA8.0和cudNN V6

    Nvidia显卡驱动安装 在ubuntu搜索框输入 软件更新,打开 "软件和更新" 对话框,在 附加驱动里选择系统检测到的Nvidia驱动,应用更改,重启系统: 安装完成之后查看G ...

  5. query更多的筛选用法

    比较操作符$eq : =  写法:db.class0.find({age:{$eq:18}},{name:1,_id:0}); 查找年龄等于18$lt :<$lte : <=$gt : & ...

  6. mysql单列索引和联合索引的使用

    1,首先要确定优化的目标,在什么样的业务场景下,表的大小等等.如果表比较小的话,可能都不需要加索引. 2,哪些字段可以建索引,一般都where.order by 或者 group by 后面的字段. ...

  7. python的pip源在windows和linux修改

    windows和linux修改python的pip源 https://www.cnblogs.com/cwp-bg/p/8497075.html windows和linux修改python的pip源 ...

  8. MacBook下java环境的搭建

    在Mac下搭建JAVA环境: 1.下载并安装JDK: 下载最新的JDK,傻瓜式安装,一直下一步就OK了. 2.配置环境变量: 在终端中输入 sudo vim ~/.bash_profile ,打开 . ...

  9. andriod的数据传递方法

    在一个主界面(主Activity)上能连接往许多不同子功能模块(子Activity上去),当子模块的事情做完之后就回到主界面,或许还同时返回一些子模块完成的数据交给主Activity处理.这样的数据交 ...

  10. 安装node

    vuejs:通过npm安装vue框架(印象深刻的问题出在了npm上) 以下所有事我自己在windows系统下遇到的问题 一.安装nodejs node下载地址https://nodejs.org/en ...