Day8-9
This commit is contained in:
11
567. Permutation in String/567. Permutation in String.csproj
Normal file
11
567. Permutation in String/567. Permutation in String.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_567._Permutation_in_String</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
9
567. Permutation in String/Program.cs
Normal file
9
567. Permutation in String/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace _567._Permutation_in_String;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine(new Solution().CheckInclusion("adc", "dcda"));
|
||||
}
|
||||
}
|
||||
39
567. Permutation in String/Solution.cs
Normal file
39
567. Permutation in String/Solution.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
namespace _567._Permutation_in_String;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public bool CheckInclusion(string s1, string s2)
|
||||
{
|
||||
int[] letters = new int[26];
|
||||
foreach (char c in s1)
|
||||
letters[c-97]++;
|
||||
int[] buff = new int[26];
|
||||
Array.Copy(letters, buff, letters.Length);
|
||||
for (int i = 0; i <= s2.Length-s1.Length; i++)
|
||||
{
|
||||
if (letters[s2[i]-97] != 0)
|
||||
{
|
||||
Array.Copy(letters, buff, letters.Length);
|
||||
for(int j = i; j < i + s1.Length; j++)
|
||||
{
|
||||
char ch = s2[j];
|
||||
buff[ch - 97]--;
|
||||
if (buff[ch-97] < 0)
|
||||
break;
|
||||
}
|
||||
bool win = true;
|
||||
for (int bIndex = 0; bIndex < buff.Length; bIndex++)
|
||||
{
|
||||
if (buff[bIndex] != 0)
|
||||
{
|
||||
win = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (win)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user