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 ...
随机推荐
- java反射教程
什么是反射,为什么它是有用的,以及如何使用它? 1.什么是反射? “反射通常是JVM中运行的程序需要检测和修改运行时程序的行为的一种能力.”这个概念通常与内省(Introspection)混淆.以下是 ...
- linux系统时间获取方式
Linux 操作系统计算系统时间:主要函数:time localtime gmtime asctime ctime mktime difftime s ...
- HDU-4035-概率dp-期望-公式化简
Maze Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submis ...
- VM虚拟机安装的XP如何全屏
首先安装install VMwear Tools..,如图:
- jquery检测input checked 控件是否被选中的方法
jquery检测input checked 控件是否被选中 js部分 复制代码代码如下: function tongyianniu(){ var gouxuan=$('input[type=check ...
- SQL (FMDB)
sql常用语句 创建表 CREATE TABLE IF NOT EXISTS "T_Person" ( "id" INTEGER NOT NULL PRIMAR ...
- emmc boot_config文件不存在
/******************************************************************************* * emmc boot_config文 ...
- 【C++基础】sort函数
sort函数的时间复杂度为O(n*logn),排序方法类似于快排. # 头文件 #include<algorithm> using namespace std; # 参数 第一个参数:要排 ...
- Git 将代码恢复到一个历史的版本
Git 将代码恢复到一个历史的版本 要把代码回到某个历史版本 比如 test有两种方法 暴力的方式 如果你的仓库是自己在用(不影响别人),那么你可以使用 git reset --hard <ta ...
- Codeforces 580B: Kefa and Company(前缀和)
http://codeforces.com/problemset/problem/580/B 题意:Kefa有n个朋友,要和这n个朋友中的一些出去,这些朋友有一些钱,并且和Kefa有一定的友谊值,要求 ...