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. kafka分区及副本在broker的分配

    kafka分区及副本在broker的分配 部分内容參考自:http://blog.csdn.net/lizhitao/article/details/41778193 以下以一个Kafka集群中4个B ...

  2. webdriver +浏览器驱动

    webdriver 经常使用的驱动有ChromeDriver , Firefox 驱动和IE驱动. 在使用的时候需要将对应的驱动下载到本地放到Python的安装路径下,然后添加路径到系统环境变量. 有 ...

  3. oracle 11g安装过程

    1,工具:Oracle_win64_11gR2_database(64位oracle 11g数据库).iso安装文件,win10系统 2,右键,解压后,点击setup.exe,系统会检测本机的环境,如 ...

  4. word转换成HTML 以及IE不兼容问题

    public static bool WordToHtml(string wordFileName, string htmlFileName) { try { Object oMissing = Sy ...

  5. python3+requests:接口自动化测试(二)

    转载请注明出处:https://www.cnblogs.com/shapeL/p/9188495.html 前言:上篇文章python3+requests+unittest:接口自动化测试(一):ht ...

  6. django之model多表操作

    一对多表之间的查询: class userInfo(models.Model): name = models.CharField(max_length=50) password = models.Ch ...

  7. JS的小判断

    // 0 if(undefined) { console.log('1'); } else { console.log('0'); } // 0 if(null) { console.log('1') ...

  8. [转]Haproxy原理(1)

    本文出处:https://www.cnblogs.com/skyflask/p/6970151.html 目录 一.四层和七层负载均衡的区别二.HAProxy与LVS的异同三.快速安装HAProxy集 ...

  9. VUE开发SPA之微信授权登录

    SPA单页应用中微信授权登录的一点思路 单页应用应该如何解决微信授权登录的尴尬跳转?后退无法返回?主要遇到的问题就是 先进入单页应用,一边渲染页面一边判断用户有没有登录,当判断到没有登录时异步数据请求 ...

  10. robot framework中的timeout的关键词

    1.默认robotframework中的含有等待的关键词(如:Wait Until Element Is Enabled),未手动设置时默认该参数为5sec 2.关键词:sleep A)一般在调试的时 ...