public class Solution
{
public string ConvertString(string pattern)
{
var dic = new Dictionary<char, int>();
int code = ;
var str = "";
for (int i = ; i < pattern.Length; i++)
{
if (!dic.ContainsKey(pattern[i]))
{
dic.Add(pattern[i], code);
str += code.ToString() + "|";
code++;
}
else
{
str += dic[pattern[i]].ToString() + "|";
}
}
return str;
} public IList<string> FindAndReplacePattern(string[] words, string pattern)
{
var pt = ConvertString(pattern);
var list = new List<string>();
foreach (var word in words)
{
var p = ConvertString(word);
if (p.Equals(pt))
{
list.Add(word);
}
}
return list;
}
}

原来的实现中没有加"|"对code进行分割,这样的代码也可以ac,但是会有隐藏的bug,那就是如下两个串会得到相同的编码:

abcdefghijklmn

abcdefghijbabbbcbd

两个字符串都会编码为012345678910111213,加了竖线就不会再有这个bug。

leetcode890的更多相关文章

  1. [Swift]LeetCode890. 查找和替换模式 | Find and Replace Pattern

    You have a list of words and a pattern, and you want to know which words in words matches the patter ...

随机推荐

  1. 秒转为时分秒格式js

    秒转为时分秒格式 function formatSeconds(value) { if(value == undefined) { value = 0; } var second = parseInt ...

  2. 获取display:none的元素的宽度和高度

    display为none的元素不能通过offsetWidth和offsetHeight来获取宽高(未参与css渲染), 解决方案:可以通过在display为none的元素使用行内样式style设置宽高 ...

  3. 在Spring Boot中使用 @ConfigurationProperties 注解 (二十六)

    @ConfigurationProperties主要作用:就是绑定application.properties中的属性 java代码 @Configuration public class DataS ...

  4. 阿里云服务器购买 发布web项目全过程

    http://blog.csdn.net/liona_koukou/article/details/50496946

  5. PHP:第五章——字符串的概念

    <?php header("Content-Type:text/html;charset=utf-8"); //字符串概念: //1.单引号.//里面的变量不会被解释 //例 ...

  6. 004——php字符串中处理函数(三)

    <?php /** * 字符串替换函数: * str_replace(); 替换字符串或数组元素,区分大小写,第四个参数可选,用于统计替换次数 * str_ireplace()不区分大小写替换 ...

  7. VS2017打包安装程序

    VS2017 并不自带安装部署项目,需要在[扩展和更新]中安装插件:Microsoft Visual Studio 2017 Installer Projects(现更名为Microsoft Visu ...

  8. JDK1.7+eclipse 4.4(luna)+pydev4.4.5构建django开发环境

    最近一直用pycharm搞django学习,但是到2017年随着版本的不断更新,启动之慢,吃资源吃内存越来越严重.果然想找一个IDE替代品. 之前用java开发分布式WEB应用,用eclipse开了N ...

  9. OMAP4之DSP核(Tesla)软件开发学习(一)

    目的:       目前手上正在OMAP4上做东西,由于涉及到大量运算,交给arm A9双核发现运算速度很慢,不能满足需求.故考虑将大量运算任务(比如FIR.FFT.卷积.图像处理.向量运算等)交给O ...

  10. MyEclipse web jsp 如何调试

    MyEclipse如何调试 | 浏览:882 | 更新:2014-03-13 17:38 1 2 3 4 5 分步阅读 当程序写好之后,如何调试呢? 我们在MyEclipse中jav添加断点,运行de ...