From 4681660c8c7b7a97f2610c53b2134ee92f3ecaa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D1=8B=D1=82=D0=BA=D0=BE=D0=B2=20=D0=A0=D0=BE=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD?= Date: Wed, 25 Mar 2026 23:58:01 +0300 Subject: [PATCH] 30. Substring with Concatenation of All Words --- .../Program.cs | 2 +- ...ing with Concatenation of All Words.csproj | 11 ++++++ .../Program.cs | 18 ++++++++++ .../Solution.cs | 36 +++++++++++++++++++ Leetcode.sln | 14 ++++++++ 5 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 30. Substring with Concatenation of All Words/30. Substring with Concatenation of All Words.csproj create mode 100644 30. Substring with Concatenation of All Words/Program.cs create mode 100644 30. Substring with Concatenation of All Words/Solution.cs diff --git a/114. Flatten Binary Tree to Linked List/Program.cs b/114. Flatten Binary Tree to Linked List/Program.cs index f37029d..29c5e34 100644 --- a/114. Flatten Binary Tree to Linked List/Program.cs +++ b/114. Flatten Binary Tree to Linked List/Program.cs @@ -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(); queue.Enqueue(root); diff --git a/30. Substring with Concatenation of All Words/30. Substring with Concatenation of All Words.csproj b/30. Substring with Concatenation of All Words/30. Substring with Concatenation of All Words.csproj new file mode 100644 index 0000000..e88807f --- /dev/null +++ b/30. Substring with Concatenation of All Words/30. Substring with Concatenation of All Words.csproj @@ -0,0 +1,11 @@ + + + + Exe + net10.0 + _30._Substring_with_Concatenation_of_All_Words + enable + enable + + + diff --git a/30. Substring with Concatenation of All Words/Program.cs b/30. Substring with Concatenation of All Words/Program.cs new file mode 100644 index 0000000..f2a78b7 --- /dev/null +++ b/30. Substring with Concatenation of All Words/Program.cs @@ -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()), + // ("barfoofoobarthefoobarman", new[] { "bar", "foo", "the" }, new[] { 6, 9, 12 }), + ("wordgoodgoodgoodbestword", new[] { "word", "good", "best", "good" }, new[] { 8 }) +}; + +string Format(IEnumerable values) => $"[{string.Join(",", values)}]"; +string FormatWords(IEnumerable 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)})"); +} diff --git a/30. Substring with Concatenation of All Words/Solution.cs b/30. Substring with Concatenation of All Words/Solution.cs new file mode 100644 index 0000000..2ec9574 --- /dev/null +++ b/30. Substring with Concatenation of All Words/Solution.cs @@ -0,0 +1,36 @@ +public class Solution { + public IList FindSubstring(string s, string[] words) { + var len = words[0].Length; + var allLen = len*words.Length; + var dict = new Dictionary(); + foreach(var word in words) + { + dict.TryAdd(word, 0); + dict[word]++; + } + var answer = new List(); + var dictClone = new Dictionary(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; + } +} \ No newline at end of file diff --git a/Leetcode.sln b/Leetcode.sln index 9df1f80..25d7a63 100644 --- a/Leetcode.sln +++ b/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