forked from BigEggStudy/LeetCode-CS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0438-FindAllAnagramsInAString.cs
38 lines (32 loc) · 1.05 KB
/
0438-FindAllAnagramsInAString.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//-----------------------------------------------------------------------------
// Runtime: 244ms
// Memory Usage: 35.4 MB
// Link: https://leetcode.com/submissions/detail/340971330/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
public class _0438_FindAllAnagramsInAString
{
public IList<int> FindAnagrams(string s, string p)
{
var sLength = s.Length;
var pLength = p.Length;
var pCount = new int[26];
foreach (var ch in p)
pCount[ch - 'a']++;
var sCount = new int[26];
var result = new List<int>();
for (int i = 0; i < sLength; i++)
{
sCount[s[i] - 'a']++;
if (i >= pLength)
sCount[s[i - pLength] - 'a']--;
if (Enumerable.SequenceEqual(sCount, pCount))
result.Add(i - pLength + 1);
}
return result;
}
}
}