Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ccbd9b6

Browse files
check in source code
1 parent 3d59475 commit ccbd9b6

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Leetcode438_FindAllAnagramsInAString
8+
{
9+
/// <summary>
10+
/// Leetcode 438 - find all anagrams in a string
11+
/// https://leetcode.com/problems/find-all-anagrams-in-a-string/description/
12+
/// </summary>
13+
class Program
14+
{
15+
static void Main(string[] args)
16+
{
17+
}
18+
19+
public static int SIZE = 26;
20+
/// <summary>
21+
///
22+
/// </summary>
23+
/// <param name="s"></param>
24+
/// <param name="p"></param>
25+
/// <returns></returns>
26+
public static IList<int> FindAnagrams(string s, string p)
27+
{
28+
if (s == null || p == null || s.Length < p.Length)
29+
return new List<int>();
30+
31+
var sLength = s.Length;
32+
var pLength = p.Length;
33+
34+
var source = getCountingSort(p);
35+
var counting = getCountingSort(s.Substring(0, pLength));
36+
var anagrams = new List<int>();
37+
if (checkAnagram(source, counting))
38+
{
39+
anagrams.Add(0);
40+
}
41+
42+
for (int i = 1; i < sLength && (pLength + i - 1) < sLength; i++)
43+
{
44+
counting[s[i - 1] - 'a']--;
45+
counting[s[pLength + i - 1] - 'a']++;
46+
if (checkAnagram(source, counting))
47+
{
48+
anagrams.Add(i);
49+
}
50+
}
51+
52+
return anagrams;
53+
}
54+
55+
private static bool checkAnagram(int[] source, int[] second)
56+
{
57+
for (int i = 0; i < SIZE; i++)
58+
{
59+
if (source[i] != second[i])
60+
return false;
61+
}
62+
63+
return true;
64+
}
65+
66+
private static int[] getCountingSort(string p)
67+
{
68+
if (p == null)
69+
{
70+
return new int[0];
71+
}
72+
73+
var counting = new int[SIZE];
74+
foreach(var item in p)
75+
{
76+
counting[item - 'a']++;
77+
}
78+
79+
return counting;
80+
}
81+
}
82+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Leetcode438_FindAllAnagramsInAString
8+
{
9+
/// <summary>
10+
/// Leetcode 438 - find all anagrams in a string
11+
/// https://leetcode.com/problems/find-all-anagrams-in-a-string/description/
12+
/// </summary>
13+
class Program
14+
{
15+
static void Main(string[] args)
16+
{
17+
}
18+
19+
public static int SIZE = 26;
20+
/// <summary>
21+
///
22+
/// </summary>
23+
/// <param name="s"></param>
24+
/// <param name="p"></param>
25+
/// <returns></returns>
26+
public static IList<int> FindAnagrams(string s, string p)
27+
{
28+
if (s == null || p == null || s.Length < p.Length)
29+
return new List<int>();
30+
31+
var sLength = s.Length;
32+
var pLength = p.Length;
33+
34+
var source = getCountingSort(p);
35+
var counting = getCountingSort(s.Substring(0, pLength));
36+
var anagrams = new List<int>();
37+
if (checkAnagram(source, counting))
38+
{
39+
anagrams.Add(0);
40+
}
41+
42+
for (int i = 1; i < sLength && (pLength + i - 1) < sLength; i++)
43+
{
44+
counting[s[i - 1] - 'a']--;
45+
counting[s[pLength + i - 1] - 'a']++;
46+
if (checkAnagram(source, counting))
47+
{
48+
anagrams.Add(i);
49+
}
50+
}
51+
52+
return anagrams;
53+
}
54+
55+
private static bool checkAnagram(int[] source, int[] second)
56+
{
57+
for (int i = 0; i < SIZE; i++)
58+
{
59+
if (source[i] != second[i])
60+
return false;
61+
}
62+
63+
return true;
64+
}
65+
66+
private static int[] getCountingSort(string p)
67+
{
68+
if (p == null)
69+
{
70+
return new int[0];
71+
}
72+
73+
var counting = new int[SIZE];
74+
foreach(var item in p)
75+
{
76+
counting[item - 'a']++;
77+
}
78+
79+
return counting;
80+
}
81+
}
82+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /