leetcode438
public class Solution
{int nextindex = ;public IList<int> FindAnagrams(string s, string p)
{
List<int> list = new List<int>();
if (s == null || s.Length == || p == null || p.Length == ) return list;
int[] hash = new int[]; //character hash
//record each character in p to hash
foreach (char c in p)
{
hash[c]++;
}
//two points, initialize count to p's length
int left = , right = , count = p.Length;
while (right < s.Length)
{
//move right everytime, if the character exists in p's hash, decrease the count
//current hash value >= 1 means the character is existing in p
if (hash[s[right++]]-- >= ) count--; //when the count is down to 0, means we found the right anagram
//then add window's left to result list
if (count == ) list.Add(left); //if we find the window's size equals to p, then we have to move left (narrow the window) to find the new match window
//++ to reset the hash because we kicked out the left
//only increase the count if the character is in p
//the count >= 0 indicate it was original in the hash, cuz it won't go below 0
if (right - left == p.Length && hash[s[left++]]++ >= ) count++;
}
return list;
}
}
https://leetcode.com/problems/find-all-anagrams-in-a-string/#/description
上面的是别人在讨论区的实现。
下面是我自己的实现,使用非递归方法,性能更好:
public class Solution
{
public IList<int> FindAnagrams(string s, string p)
{
var list = new List<int>();
var dicp = new Dictionary<char, int>();
foreach (var c in p)
{
if (dicp.ContainsKey(c))
{
dicp[c]++;
}
else
{
dicp.Add(c, );
}
}
var dictemp = new Dictionary<char, int>(dicp);
var counttemp = p.Length;
var beginindex = ;
for (var i = ; i < s.Length; i++)
{
var c = s[i];
if (dictemp.ContainsKey(c))
{
if (dictemp[c] > )
{
dictemp[c]--;
counttemp--;
if (counttemp == )
{
list.Add(beginindex); //restore status
dictemp[s[beginindex]]++;
beginindex = beginindex + ;
counttemp++;
}
}
else
{
if (s[beginindex] == c)
{
beginindex = beginindex + ;
}
else
{
beginindex = i;
dictemp = new Dictionary<char, int>(dicp);
dictemp[c]--;
counttemp = p.Length - ;
}
}
}
else
{
beginindex = i + ;
dictemp = new Dictionary<char, int>(dicp);
counttemp = p.Length;
}
}
return list;
}
}
leetcode438的更多相关文章
- [Swift]LeetCode438. 找到字符串中所有字母异位词 | Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- Leetcode438.Find All Anagrams in a String找到字符串中所有字母异位词
给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引. 字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 20100. 说明: ...
随机推荐
- 关于Java的特点之多态
多态--概念 所谓多态,就是指一个引用(类型)在不同情况下的多种状态.也可以理解成:多态是指通过指向父类的指针,来调用在不同子类中实现的方法. 实现多态有两种方式:1.继承:2.接口 多态--注意事项 ...
- makeObjectsPerformSelector对数组中的对象发送消息执行对象中方法
- (void)makeObjectsPerformSelector:(SEL)aSelector; - (void)makeObjectsPerformSelector:(SEL)aSelector ...
- 【原创】MIPS相关
MIPS是单字长定点指令平均执行速度 Million Instructions Per Second的缩写. 路由器等嵌入式系统多采用MIPS和ARM两种指令架构,最近在研究路由器,借机总结一下基于M ...
- cython 成功创建import 模块
又是因为别人代码里有这么一个部分,用到了cython,,简而言之,就是利用这个模块调用C语言,从而加速程序运行,其中具体怎么调用我还没整清楚,但基本用法差不多了解了. 1 安装:https://www ...
- easyui datagrid去掉全选按钮
第一步: F12查看元素,选中全选按钮,把全选按钮的class里边加上display:none属性.找到对应的class,即.datagrid-header-check. 第二步: 在加载 表格的时候 ...
- 常见模块(三) pickle模块和json模块
(一)json和pickle的区别 1.json是不同编程语言实现数据交换的工具,他是用来把python中的数据对象转换成字符串或者写入文件中的,再由其他语言通过json加载进来. 2.pickle是 ...
- cordova最小化app插件的使用: cordova-plugin-appminimize
1. 添加插件:cordova plugin add cordova-plugin-appminimize 2.调用方法: window.plugins.appMinimize.minimize(); ...
- zombodb query dsl
zombodb query dsl 是为了简化es 查询的处理,同时可以兼容基本上所有的es 操作 一个简单的查询,查询任何字段包含cats 以及dogs 的 SELECT * FROM table ...
- Spark Streaming 'numRecords must not be negative'问题解决
转载自:http://blog.csdn.net/xueba207/article/details/51135423 问题描述 笔者使用spark streaming读取Kakfa中的数据,做进一步处 ...
- The query below helps you to locate tables without a primary key:
SELECT tables.table_schema, tables.table_name, tables.table_rows FROM information_schema.tables LEFT ...