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的更多相关文章

  1. [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 ...

  2. Leetcode438.Find All Anagrams in a String找到字符串中所有字母异位词

    给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引. 字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 20100. 说明: ...

随机推荐

  1. get 乱码解决方案

    1.针对Post方式提交的请求如果出现乱码,可以每次在request解析数据时设置编码格式: request.setCharacterEncoding("utf-8"); 也可以使 ...

  2. input做一个开关按钮

    .mui-switch { width: 52px; height: 31px; position: relative; border: 1px solid #dfdfdf; background-c ...

  3. tail语法

    本文介绍Linux下tail命令的使用方法.linux tail命令用途是依照要求将指定的文件的最后部分输出到标准设备,通常是终端,通俗讲来,就是把某个档案文件的最后几行显示到终端上,假设该档案有更新 ...

  4. python实现算术表达式的词法语法语义分析(编译原理应用)

    本学期编译原理的一个大作业,我的选题是算术表达式的词法语法语义分析,当时由于学得比较渣,只用了递归下降的方法进行了分析. 首先,用户输入算术表达式,其中算术表达式可以包含基本运算符,括号,数字,以及用 ...

  5. Windows10 VS2017 C++ ini解析(使用simpleini头文件)

    simpleini项目地址: https://github.com/brofield/simpleini 下载,新建项目,并将SimpleIni.h文件通过包含目录的方式加载进来. 创建test.in ...

  6. Python函数定义、文件操作(读写、修改)

    date:2018421 day3 一.函数定义 def  函数名(首字母大写) 增加程序可读性 #直接写函数名即可调用函数 #参数 ①.形参 ②.实参 ③.缺省参数 如果有默认值,调用的时候没有传递 ...

  7. Gym101889E. Enigma(bfs+数位)

    比赛链接:传送门 题目大意: 求一个十进制大数S(有部分数位为"?")能被N整除时的最小值,如果没有办法被N整除,输出"*". 思路: 一个数位上的数值增加1后 ...

  8. Javascript内置对象、原生对象、宿主对象关系

    https://blog.csdn.net/foamflower/article/details/9165691

  9. message box

    QMessageBox 弹出框上的按钮设置为中文   Qt 默认的弹出框上的按钮式英文,虽然也知道是什么意思,但终究不如中文看着顺眼. QMessageBox box(QMessageBox::War ...

  10. spring mvc 为什么这么多xml

    spring web mvc 处理流程 Architecture web.xml (webapp必要配置) 作用:spring web mvc 使用dispatcherServlet 分发reques ...