30. Substring with Concatenation of All Words
This commit is contained in:
@@ -22,7 +22,7 @@ static TreeNode? BuildTreeFromLevelOrder(int?[] data)
|
||||
if (data.Length == 0 || data[0] == null)
|
||||
return null;
|
||||
|
||||
var root = new TreeNode(data[0].Value);
|
||||
var root = new TreeNode(data[0]!.Value);
|
||||
var queue = new Queue<TreeNode>();
|
||||
queue.Enqueue(root);
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<RootNamespace>_30._Substring_with_Concatenation_of_All_Words</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
18
30. Substring with Concatenation of All Words/Program.cs
Normal file
18
30. Substring with Concatenation of All Words/Program.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
var sol = new Solution();
|
||||
|
||||
var cases = new (string s, string[] words, int[] expected)[]
|
||||
{
|
||||
// ("barfoothefoobarman", new[] { "foo", "bar" }, new[] { 0, 9 }),
|
||||
// ("wordgoodgoodgoodbestword", new[] { "word", "good", "best", "word" }, Array.Empty<int>()),
|
||||
// ("barfoofoobarthefoobarman", new[] { "bar", "foo", "the" }, new[] { 6, 9, 12 }),
|
||||
("wordgoodgoodgoodbestword", new[] { "word", "good", "best", "good" }, new[] { 8 })
|
||||
};
|
||||
|
||||
string Format(IEnumerable<int> values) => $"[{string.Join(",", values)}]";
|
||||
string FormatWords(IEnumerable<string> values) => $"[{string.Join(",", values)}]";
|
||||
|
||||
foreach (var (s, words, expected) in cases)
|
||||
{
|
||||
var actual = sol.FindSubstring(s, words);
|
||||
Console.WriteLine($"{s} | {FormatWords(words)} -> {Format(actual)} (expected: {Format(expected)})");
|
||||
}
|
||||
36
30. Substring with Concatenation of All Words/Solution.cs
Normal file
36
30. Substring with Concatenation of All Words/Solution.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
public class Solution {
|
||||
public IList<int> FindSubstring(string s, string[] words) {
|
||||
var len = words[0].Length;
|
||||
var allLen = len*words.Length;
|
||||
var dict = new Dictionary<string, int>();
|
||||
foreach(var word in words)
|
||||
{
|
||||
dict.TryAdd(word, 0);
|
||||
dict[word]++;
|
||||
}
|
||||
var answer = new List<int>();
|
||||
var dictClone = new Dictionary<string, int>(dict);
|
||||
for(var i = 0; i < s.Length-allLen+1; i++)
|
||||
{
|
||||
var valid = 0;
|
||||
foreach (var kvp in dictClone)
|
||||
dictClone[kvp.Key] = dict[kvp.Key];
|
||||
for(var j=i; j < i+allLen; j+=len)
|
||||
{
|
||||
var word = s[j..(j+len)];
|
||||
if(dictClone.TryGetValue(word, out var count) && count > 0)
|
||||
{
|
||||
dictClone[word]--;
|
||||
valid++;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(valid == words.Length)
|
||||
answer.Add(i);
|
||||
}
|
||||
return answer;
|
||||
}
|
||||
}
|
||||
14
Leetcode.sln
14
Leetcode.sln
@@ -195,6 +195,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "32. Longest Valid Parenthes
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "114. Flatten Binary Tree to Linked List", "114. Flatten Binary Tree to Linked List\114. Flatten Binary Tree to Linked List.csproj", "{D86CCC06-597D-4DF8-8DCC-E7804E773530}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "30. Substring with Concatenation of All Words", "30. Substring with Concatenation of All Words\30. Substring with Concatenation of All Words.csproj", "{DC410457-B769-4597-857D-5AAD2BDBAAC3}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -1357,6 +1359,18 @@ Global
|
||||
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Release|x64.Build.0 = Release|Any CPU
|
||||
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Release|x86.Build.0 = Release|Any CPU
|
||||
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Release|x64.Build.0 = Release|Any CPU
|
||||
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
Reference in New Issue
Block a user