Day i do not know

This commit is contained in:
Electrominch
2022-10-18 00:45:31 +03:00
parent 01c5720116
commit 284b6ff10c
72 changed files with 1256 additions and 12 deletions

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_438._Find_All_Anagrams_in_a_String</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,10 @@
namespace _438._Find_All_Anagrams_in_a_String;
internal class Program
{
static void Main(string[] args)
{
var res = new Solution().FindAnagrams("cbaebabacd", "abc");
Console.WriteLine(String.Join(" ", res));
}
}

View File

@@ -0,0 +1,35 @@
namespace _438._Find_All_Anagrams_in_a_String;
public class Solution
{
public IList<int> FindAnagrams(string s, string target)
{
List<int> result = new List<int>();
int[] array = new int[26];
int[] buf = new int[26];
foreach(var ch in target)
array[ch-97]+=1;
for(int i = 0; i <= s.Length-target.Length; i++)
{
int chIndex = s[i]-97;
if (array[chIndex] != 0)
{
Array.Copy(array, buf, array.Length);
bool win = true;
for (int j = i; j < i + target.Length; j++)
{
int chIndex2 = s[j] - 97;
buf[chIndex2] -= 1;
if (buf[chIndex2] < 0)
{
win = false;
break;
}
}
if (win)
result.Add(i);
}
}
return result;
}
}